Kex
Kex

Reputation: 776

Cocos2D 2.0 ARC enabled uncontrolled deallocs

I recently migrated an existing Cocos2D project from version 0.8 to 2.0 & enabled ARC.

The way I did it is by Apple's empty application template & then adding the code from the Cocos2d 2.x template since it has major changes. After that I added code from the game & made the necessary changes for the deprecated code & for the ARC issues.

Since that the game is working but not as expected, I had no animations & the game was taking the whole CPU power. From the console I saw that everything gets dealloc-ed right after it's creation. My old code is not the reason for that because it even happens before any of my scenes gets pushed.

enter image description here

EDIT I also repeated again the whole process & made an ARC-enabled version from the Cocos2D template project, but the same there too.. Is that a normal thing maybe?

Upvotes: 2

Views: 271

Answers (1)

CodeSmile
CodeSmile

Reputation: 64478

That's not normal, although common problem when converting to ARC. ARC will release objects out of scope, whereas under MRC an alloc/init object would stay in memory (and leak). Check where you may need to keep a strong reference.

Here's an example that worked before converting to ARC:

-(void) someMethod
{
    id object = [[MyObject alloc] init];
}

Under MRC, object stays in memory (leaks) after someMethod returns. Under ARC, ARC cleans up the object when the method returns. The simplest fix is to turn object into an ivar (aka instance variable, member of class).

Also check singletons. Depending on how its implemented, the Singleton class might dealloc right away. For example if the static instance is declared __weak or __unsafe_unretained.

You should also run the Xcode Analyzer (Build -> Analyze) to get pointers for potential issues.

Upvotes: 1

Related Questions