问题描述:
昨天晚上通宵改Geely iPad的bug,这个bug改了8个小时。问题是程序会泄露导致崩溃。
问题分析:
项目使用的是cocos2d,所以也不能使用Instruments做性能测试。一步一步发现一个CCLayer没有dealloc,于是进去一步一步查,一段段的注释和启用。最后发现有了这段代码就会出现泄露崩溃:
[vBackground stopAllActions];
[vMenu stopAllActions];
id backgroundFaceAction=[CCFadeOut actionWithDuration:0.7];
id backgroundCallAction=[CCCallFuncN actionWithTarget:self selector:@selector(removeNode:)];
id backgroundSequenceAction=[CCSequence actions:backgroundFaceAction, backgroundCallAction, nil];
id menuFaceAction=[CCFadeOut actionWithDuration:0.7];
id menuCallAction=[CCCallFuncN actionWithTarget:self selector:@selector(removeNode:)];
id menuSequenceAction=[CCSequence actions:menuFaceAction, menuCallAction, nil];
[vBackground runAction:backgroundSequenceAction];
[vMenu runAction:menuSequenceAction];
其中调用的removeNode函数如下:
-(void) removeNode:(id)vNode
{
CCNode *node=vNode;
if (node.parent!=nil)
{
[self removeChild:node cleanup:YES];
}
}
改成直接removeChild掉两个CCSprite,就不崩溃了:
[self removeNode:vBackground];
[self removeNode:vMenu];
问题解决:猜想是两个CCSprite同一时间开始的两个CCAction后同时调用removeNode函数,但是一个调到了,一个调用不到。想来这个是一个cocos2d的bug。