C++ Cocos2d替换场景不释放旧场景
我有一个box2d-cocos2d游戏,有几个关卡。“我的级别”是使用从其他类继承的Box2d功能的图层。例如,第一层看起来像:C++ Cocos2d替换场景不释放旧场景,c++,iphone,ios,objective-c,cocos2d-iphone,C++,Iphone,Ios,Objective C,Cocos2d Iphone,我有一个box2d-cocos2d游戏,有几个关卡。“我的级别”是使用从其他类继承的Box2d功能的图层。例如,第一层看起来像: Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject *** DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1> 2013-08-26 12:05:57.089 Myapp[6334:907] ***BOX2DLEVEL DE
Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject
*** DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1>
2013-08-26 12:05:57.089 Myapp[6334:907]
***BOX2DLEVEL DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1>
2013-08-26 12:05:57.093 Myapp[6334:907] ***BOX2DLAYER DEALLOC ***
<Level1Layer = 0x1cdcfe70 | Tag = -1>
我的问题是该层没有在应该释放的时候释放。例如,如果我使用此代码重放该层:
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5f scene:[[self class] node]]];
然后内存占用就增加了。如果我有一个像“菜单->级别1->菜单->级别1”这样的圆圈,我的应用程序在一段时间后因为内存不足而崩溃,也会发生同样的情况。我用NSLogs跟踪dealloc方法,但在我替换场景时会调用每个dealloc方法。
我的日志看起来像:
Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject
*** DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1>
2013-08-26 12:05:57.089 Myapp[6334:907]
***BOX2DLEVEL DEALLOC ***<Level1Layer = 0x1cdcfe70 | Tag = -1>
2013-08-26 12:05:57.093 Myapp[6334:907] ***BOX2DLAYER DEALLOC ***
<Level1Layer = 0x1cdcfe70 | Tag = -1>
Box2dLevel解除锁定:
-(void) dealloc{
NSLog(@"\n***BOX2DLEVEL DEALLOC ***%@",self);
[self removeChild:_label cleanup:YES];
[self removeChild:labelForCoins cleanup:YES];
[self removeChild:labelForTime cleanup:YES];
[self removeChild:freezeCountDown cleanup:YES];
[self removeChild:freezedMask cleanup:YES];
[self removeChild:_backGround cleanup:YES];
[self removeChild:darkLayer cleanup:YES];
[self removeChild:flashlayer cleanup:YES];
[self removeChild:skillsMenu cleanup:YES];
[arrayForObjects release];
[skillsMenuArray release];
[super dealloc];
}
Box2dLayer解除锁定:
-(void) dealloc
{
NSLog(@"***BOX2DLAYER DEALLOC ***\n%@",self);
if (debugDraw) {
delete debugDraw;
debugDraw = NULL;
}
if (mouseJoint) {
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
if (groundBody) {
world->DestroyBody(groundBody);
groundBody = NULL;
}
if (referenceBody) {
world->DestroyBody(referenceBody);
referenceBody = NULL;
}
if (bombSensor) {
world->DestroyBody(bombSensor);
bombSensor = NULL;
}
if (laserSensor) {
world->DestroyBody(laserSensor);
laserSensor = NULL;
}
if (_contactListener) {
free(_contactListener);
_contactListener = NULL;
}
[self removeChild:_obj cleanup:YES];
[super dealloc];
NSLog(@"\n***Box2dLayer superclass deallocated");
尝试使用
[Level1layer scene]
而不是[[self class]节点]
(示例项目也是如此)使用CCTextureCache转储方法,如果每次调用dealloc日志时都看到它,那么可能只是加载了太多的纹理,而不是像LearnCos2D所说的那样在其他地方占用了大量内存,或者dealloc方法中的某些内容在不知不觉中失败了。你能发布你的dealoc方法吗?@giorashc我发布了dealoc方法。似乎没有调用box2dLayer的[super dealloc]。您是否尝试一步一步地调试它?在dealloc链中,它在哪一行失败?(顺便说一句,在您的dealloc级别中,您调用removeAllChildrenWithCleanup,因此在使用removeChild的super dealloc方法中再这样做是没有用的)您确定没有保留它们的CCDictionary或CCArray类吗。对象无法释放的唯一原因是其保留计数不是“0”。尝试在DealLocal中添加重新登录的日志。这应该会给我们一些启示。