I'm a Java coder. That right there should tell you I'm in trouble :) I did do C/C++ professionally for many years prior to Java but for the past 7 years, all Java.
So, I'm having memory issues and the leak finder is definitely finding them for me plus I'm having some weird artifacts. Let me give the rundown of how the code is working and I would very much appreciate any help.
[Game Initialization]
Background Layer: Create 2 CCSpriteBatchNodes and add them as children to the layer
CCSpriteBatchNode *currentMainBackgroundSpriteBatch = [CCSpriteBatchNode batchNodeWithTexture:[[CCTextureCache sharedTextureCache] addImage:mainBackground]];
CCSpriteBatchNode *currentBufferBackgroundSpriteBatch = [CCSpriteBatchNode batchNodeWithTexture:[[CCTextureCache sharedTextureCache] addImage:bufferBackground]];
... add CCSprites
[self addChild:currentMainBackgroundSpriteBatch z:0 tag:MainBackgroundCache];
[self addChild:currentBufferBackgroundSpriteBatch z:0 tag:BufferBackgroundCache];
[/Game Initialization]
[Game Loop]
Background Layer: Every frame, move the 2 CCSpriteBatchNodes to simulate movement
CCSpriteBatchNode *mainBackground = (CCSpriteBatchNode *)[self getChildByTag:MainBackgroundCache];
CCSpriteBatchNode *bufferBackground = (CCSpriteBatchNode *)[self getChildByTag:BufferBackgroundCache];
CGPoint mainPos = mainBackground.position;
CGPoint bufferPos = bufferBackground.position;
mainPos.y -= speed;
bufferPos.y -= speed;
mainBackground.position = mainPos;
bufferBackground.position = bufferPos;
[CONDITION: X TIME HAS PASSED]
Spawn Background thread to initialize 2 new CCSpriteBatchNodes with new background images:
CCSpriteBatchNode *nextMainSpriteBatch = [CCSpriteBatchNode batchNodeWithTexture:[[CCTextureCache sharedTextureCache] addImage:mainBackground]];
CCSpriteBatchNode *nextBufferSpriteBatch = [CCSpriteBatchNode batchNodeWithTexture:[[CCTextureCache sharedTextureCache] addImage:bufferBackground]];
... add CCSprites
When completed, call a method on the BackgroundLayer passing the 2 new CCSpriteBatchNodes to it and then set a flag saying the new backgrounds are ready.
[[BackgroundLayer instance] setNextTerrainBuffers:nextMainSpriteBatch
andBufferTerrain:nextBufferSpriteBatch];
After this call I do nothing but exit (i.e. I do NOT release these batch nodes as I need them to stay in memory for the background layer
Inside BackgroundLayer, the setNextTerrainBuffers looks like this:
-(void) setNextTerrainBuffers:(CCSpriteBatchNode *)mainTerrain
andBufferTerrain:(CCSpriteBatchNode *)bufferTerrain {
nextMainBackgroundSpriteBatch = [mainTerrain retain];
nextBufferBackgroundSpriteBatch = [bufferTerrain retain];
}
While this thread has been running, BackgroundLayer has been checking a bool to see if the thread was finished. Once it is, it does the following:
: Waits for one of the scrolling batch nodes to go out of visual range. When it does it removes the existing batch node from the layer and replaces it by doing:
CCSpriteBatchNode *mainBatchNode = (CCSpriteBatchNode *)[self getChildByTag:MainBackgroundCache];
CGPoint position = mainBatchNode.position;
position.y = position.y - backgroundOffset;
nextMainBackgroundSpriteBatch.position = position;
if(mainBatchNode) {
[mainBatchNode removeAllChildrenWithCleanup:TRUE];
[self removeChildByTag:MainBackgroundCache cleanup:TRUE];
}
[self addChild:nextMainBackgroundSpriteBatch z:0 tag:MainBackgroundCache];
: Waits for the other scrolling batch node to go out of visual range and does the same code above but with other "new" batch node.
[/CONDITION: X TIME HAS PASSED]
[/Game Loop]
So, in a nutshell, what I am doing is creating 2 batch nodes in the main thread. When I need to replace them, I create 2 new batch nodes, remove (and clean) the first batch nodes from the layer and then add the 2 new batch nodes in the old one's place.
I do not have autorelease or release anywhere in my code for these as I was under the assumption that the [self removeChildByTag:TAG cleanup:TRUE] call would do that for me.
Am I in trouble?
Thanks!
PAR