2013-08-26 34 views
1

我有一個box2d-cocos2d遊戲,有幾個級別。我的級別是使用從另一個類繼承的Box2d功能的圖層。例如,第一層的樣子:Cocos2d replaceScene不釋放舊場景

Level1Layer : Box2DLevel : Box2DLayer : CCLayer : CCNode : NSObject 

我的問題是,如果我重播水平與此代碼層沒有釋放時should.For例如:

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

然後內存佔用只是增加。同樣的事情也發生,如果我有一個圓形菜單 - >級別1 - >菜單 - >級別1和我的應用程序只是在一段時間後崩潰,因爲memory.I跟蹤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級的dealloc:

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

Box2dLevel的dealloc:

-(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的dealloc:

-(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"); 
+1

使用CCTextureCache轉儲方法,也許你只是有太多的紋理加載 – LearnCocos2D

+0

好吧,如果你每次看到一個dealloc日誌應該被調用,比你可能在其他地方有一個大的內存使用量LearnCocos2D或可能在你的dealloc方法沒有你知道它失敗。你可以發佈你的dealloc方法嗎? – giorashc

+0

@giorashc我發佈了dealloc方法。看來box2dLayer的[super dealloc]沒有被調用。 – kommancs96

回答

0

嘗試使用的[Level1layer scene]代替[[self class] node](所以做示例項目做到這一點)