Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone cocos2d sprites in array, memory problems

I'm trying to keep track of my sprites in an array, add and remove them from layers, and then finally clear them out of the array.

I'm using the following code:

Sprite * Trees[50];
Layer * Forest;

Forest =  [Layer node];
Forest.isTouchEnabled = YES;
[self addChild:Forest z:30];

// do this a bunch of times
Trees[0] = [[Sprite spriteWithFile:@"mytree.png"] retain];
[Trees[0] setPosition:cpv(240,160)];
[Forest addChild:Trees[0] z:5];

And then when I want to destroy a tree I use:

[Forest removeChild:Trees[0] cleanup:YES];
[Trees[0] release];

My problem is that when I look in Instruments, I'm never reclaiming that memory, there is never a drop back down. I thought that by releasing the sprite it would free up the memory. Am I doing this completely wrong?

like image 203
Patrick Avatar asked Apr 25 '26 09:04

Patrick


1 Answers

I know that when you are using the simulator with cocos2d, the memory doesn't look like it's being released, so you have to run it on the device to get an accurate picture of what's going on.

There is a good discussion here about cocos2d and memory.

What I've noticed is that everything that you create and retain must be released, but it isn't released from memory until I do this:

[[TextureMgr sharedTextureMgr] removeAllTextures]; 

That will release the memory.

Here's a bigger example:

Sprite * sPopup = [[Sprite spriteWithFile:@"popup.png"] retain];
    sPopup.position = cpv(240,440);
    [self addChild: sPopup z:2];
[sPopup release];

Then, when I'm done with sPopup in another function I have this:

[[TextureMgr sharedTextureMgr] removeAllTextures]; 

and the memory is freed.

like image 193
Bryan Cimo Avatar answered Apr 26 '26 21:04

Bryan Cimo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!