【问题标题】:Cocos2d replaceScene not releasing the old sceneCocos2d replaceScene 不释放旧场景
【发布时间】:2013-08-26 09:16:02
【问题描述】:

我有一个 box2d-cocos2d 游戏,它有几个级别。我的关卡是使用从另一个类继承的 Box2d 特性的层。例如第一层看起来像:

Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject

我的问题是该层没有在应该释放的时候释放。例如,如果我使用以下代码重放关卡:

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5f scene:[[self class] node]]];

然后内存占用只会增加。如果我有一个类似菜单 -> 级别 1 -> 菜单 -> 级别 1 的圆圈并且我的应用程序在一段时间后由于内存不足而崩溃,也会发生同样的事情。我跟踪了 dealloc NSLogs 的方法,但是当我替换场景时,每个 dealloc 方法都会被调用。 我的日志看起来像:

*** 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> 

我真的被卡住了,因为在 iPhone 4 上玩了 3 个关卡后,应用程序崩溃了。 我怎么解决这个问题?任何解决方案和指针表示赞赏。

1 级释放:

-(void)dealloc{
    NSLog(@"\n*** DEALLOC ***%@",self);
    [self removeAllChildrenWithCleanup:YES];
    [super dealloc];
}

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");

【问题讨论】:

  • 使用CCTextureCache转储方法,也许你只是加载了太多纹理
  • 好吧,如果您每次应该调用它时都看到一个 dealloc 日志,那么您可能会在其他地方使用大量内存,如 LearnCocos2D 所述,或者您的 dealloc 方法中的某些内容在您不知情的情况下失败了。你能发布你的 dealloc 方法吗?
  • @giorashc 我发布了 dealloc 方法。好像没有调用box2dLayer的[super dealloc]。
  • 你尝试一步步调试了吗?它在 dealloc 链中的哪一行失败? (顺便说一句,在您的级别 dealloc 中,您调用 removeAllChildrenWithCleanup 因此在使用 removeChild 的超级 dealloc 方法中再次这样做是没有用的)
  • 您确定吗,您没有保留它们的 CCDictionary 或 CCArray 类。一个对象不会被释放的唯一原因是它的保留计数不是“0”。尝试在你的 dealloc 中为 retainCount 添加日志。这应该会提供一些见解。

标签: c++ iphone ios objective-c cocos2d-iphone


【解决方案1】:

尝试使用[Level1layer scene] 而不是[[self class] node](示例项目也是这样做的)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多