我有一些看起来像这样的代码:
actualColor = 0;
targetColors = [NSArray arrayWithObjects:[UIColor blueColor],
[UIColor purpleColor],
[UIColor greenColor],
[UIColor brownColor],
[UIColor cyanColor], nil];
timer = [NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:@selector(switchScreen)
userInfo:nil
repeats:YES];
Run Code Online (Sandbox Code Playgroud)
在选择器中我有这个:
- (void) switchScreen
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
int totalItens = [targetColors count];
NSLog(@"Total Colors: %i",totalItens);
if(actualColor >= [targetColors count])
{
actualColor = 0;
}
UIColor *targetColor = [targetColors objectAtIndex:actualColor];
if(!firstUsed)
{
[firstView setBackgroundColor:targetColor];
[secondView setAlpha:0.0];
[firstView setAlpha:1.0];
firstUsed = YES;
}
else
{
[firstView setBackgroundColor:targetColor];
[secondView setAlpha:1.0];
[firstView setAlpha:0.0]; …Run Code Online (Sandbox Code Playgroud) 我正在使用Cocos2D for iOS开发游戏.
有一些场景,如菜单等和主要的游戏场景.在主场景中只有三个动态对象.这些物体定期相互射击(直到这些物体被杀死或移出场景).
现在的问题是:游戏不断地耗尽记忆力.我想知道我做错了什么.
没有明显的泄漏,如过度保留的物体.场景被dealloc编辑,对象从父母移除并清理,动画停止等.
无论如何,记忆继续在某个地方.我正在使用以下代码
+ (void) reportMemory
{
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if (kerr == KERN_SUCCESS)
NSLog(@"Memory in use (in Kbytes): %f", info.resident_size / 1024.0);
else
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
Run Code Online (Sandbox Code Playgroud)
找出每次开始场景时消耗的内存量.报告的数字总是比前一个更大.
我试图使用分配探查器,但说实话,我无法弄清楚任何有用的东西.我看到总生存字节基本相同,但是进程不断分配和释放一些东西.
你建议我看一下什么?基本上,我正在寻求如何在我的情况下调试内存操作的建议.
编辑(有什么帮助我):
事实证明我打开了NSZombieEnabled.基本上,它是恒定内存消耗增加的主要因素.一些有用的信息和提示可以在@coneybeare答案中找到
第二个最有用的是使用@Jack建议的仪器(Leaks and Allocations).它帮助我找到了几个微妙的泄漏.
有时我将NSZombieEnabled环境参数设置为YES以进行调试.
这次我忘了关闭选项,并将我的应用程序提交到App Store.
我想这个选项只是一个环境选项,所以并不影响提交的应用程序.
我对么?
iphone ×2
cocoa ×1
debugging ×1
ios ×1
memory-leaks ×1
objective-c ×1
profiling ×1
xcode4.2 ×1