1
votes

I am using Cocos2d(ARC). i am making a game in which i am using custom CCSprite class

+(id)createMySprite
{
   return [[self alloc] initMy] ;
}

This is my CCSprite Class . Where i allocate the memory. I can't write autorelease because of ARC. I have two CCLayer Classes. In first class has just play button with click of that button it replaces the screen and moves second screen. In 2nd Class I fetch to many Custom CCSprite Classes. When i replace to 1st CCLayer class, it never release the memory of 2nd class and memory will continuously increase. Then Crash after reach to 20-30 MB Live memory. So I, need help regarding this.

Allocation

Basically, headers are ARC compatible. Source code does not support ARC. So, you compile Cocos2D as a static lib without enabling ARC and then you link to it in your code in which you can safely use ARC for your classes and custom subclasses of Cocos2D elements. Please correct me if I am wrong, but there should be no problem with using ARC in your project and have say a CCSprite subclass which uses ARC and does not manually place retain and release messages. Right?

1
You are seeing allocations, not leaks, In instruments under memory select leaks to know if you are really leaking memory.Ramy Al Zuhouri
To release an object you set the pointer to it to nil. Btw what do you mean with: "When i replace to 1st CCLayer class"?Ramy Al Zuhouri
Need to see the code how you replace those layers. You're most certainly keeping a strong reference to your custom sprite objects somehow (common issue being to use a singleton class to hold on to certain objects for whatever reason). You might want to override the dealloc method, add a NSLog statement and set a breakpoint to see if they actually get dealloc'd. If not there's still a strong reference. If they do get dealloc'd, the additional memory is coming from somewhere else.LearnCocos2D
Your sprite class is creating its own sprite sheet and adding it to itself, not the layer. This may be your problem. Add the sprite to the layer directly or add the sprite sheet to the layer then add the sprite to it. As it looks right now, the sprite class is not in any way a child of the scene so it won't be called to dealloc when the scene exits.Sylvan
Not really sure why you're having a problem. The layer should create a CCSpriteBatchNode and any sprite or other sprite-derived classes should be added as children to the batch node.Sylvan

1 Answers

0
votes

I know this has been around for quite sometime and most likely you have resolved it but for those whom are reading this and are still looking for an answer, here is my understanding of CCNodes and retain cycles.

Basically I have blog'ed about this and I have a design pattern that gets around over retained CCNodes. Here is the link. http://www.rotategears.com/development/ccnode-arc-memory-management/

In a nut shell, if you create a node ivar and don't specify the strength of the reference, it will get a strong reference. Then when you add it as a child to the node that will also maintain the reference, you would have created a double retain which causes problems when the OS tries to release it. The ivar must be week so that you only have a retain count of 1 in this container. Having said that, you then have to be carefull how you assign this weak reference to the ivar as it will need a reference count of 1 before you assign it or else the ivar will be set to nil.