开发者

cocos2d dealloc of a scene is never called after replaceScene

开发者 https://www.devze.com 2023-03-27 16:28 出处:网络
I have a GameScene where I add a large image as child. I want to re-create the GameScene for every level. But after several levels\' game play, it receives memory warning and eventually get crashed. T

I have a GameScene where I add a large image as child. I want to re-create the GameScene for every level. But after several levels' game play, it receives memory warning and eventually get crashed. The开发者_如何学Go dealloc method is never get called.

start with MainScene, when hit 'start' button->

[[CCDirector sharedDirector] replaceScene: [GameScene scene]];

when clear the level->

[[CCDirector sharedDirector] replaceScene: [WinScene scene]];

when hit 'next' button->

[[CCDirector sharedDirector] replaceScene: [GameScene scene]];

etc... But the dealloc of GameScene is never fired. (MainScene and WinScene are fine)

for the GameScene I have a

static GameScene* instanceOfGameScene;

other relevant methods: (I dont think it is the instanceOfGameScene that retained the GameScene, since it is a common approach for me to do, OK in my other projects. )

-(id) init {
if ((self = [super init])) {

    instanceOfGameScene = self;
etc...


-(void) dealloc {
    CCLOG(@"game scene get dealloc'ed");
    instanceOfGameScene = nil;
    [super dealloc];
}



+(GameScene*) sharedScene
{
    return instanceOfGameScene;
}

In the WinScene, I perform [[GameScene sharedScene] removeAllChildrenWithCleanup: YES] 5 seconds later, the dealloc method of GameScene is finally fired, with Program received signal: "EXC_BAD_ACCESS". in the last line of -(void) removeAllChildrenWithCleanup:(BOOL)cleanup method in CCNode class:

[children_ removeAllObjects];

stack deeper the error is CCArray removeAllObjects:

ccArrayRemoveAllObjects(data);

then is:

/** Removes all objects from arr */
static inline void ccArrayRemoveAllObjects(ccArray *arr)
{
    while( arr->num > 0 )
        [arr->arr[--arr->num] release]; 
}


problem solved. I put the buttons (with block declaration, which may retain some objects of the GameScene) into the onEnter method, and remove it on the onExit method, instead of just putting all stuff inside init method. it may sound weird, but it works now. ^_^Happy after a whole day's debugging.


There's no reason for GameScene to be a singleton; certainly not a poorly created singleton. Most certainly not a poorly created singleton that unsets itself before [super dealloc] can be called. Absolutely most certainly not a poorly created singleton that's not even being used properly. The purpose of a singleton is to have a class that's initialized ONCE over the course of the application, not the life of the class itself.

If fixing the singleton so it's a regular class doesn't fix the problem, then your problem is in code you haven't shown us.

Also, you could hit CCTextureCache to release unused textures after your dealloc your GameScene. That'll help you with the memory aspect.

0

精彩评论

暂无评论...
验证码 换一张
取 消