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 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];
}