How To Enable ARC in a Cocos2D 2.X Project

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

How To Enable ARC in a Cocos2D 2.X Project

Postby rwenderlich » Tue Nov 20, 2012 11:00 am

This is the official thread to discuss the following blog post: How To Enable ARC in a Cocos2D 2.X Project
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2230
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 366 times

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby Romyilano » Wed Nov 21, 2012 2:41 am

Thank you so much for posting this! I almost gave up doing some of the Cocos2D tutorials, silly me. Other methods to make Cocos2D arc compatible were blowing up my computer.

Where would newbies be without this site?
Romyilano
Subscriber since Mar 30, 2014
n00b
 
Posts: 3
Joined: Fri Sep 07, 2012 9:00 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby mojo » Wed Nov 21, 2012 8:18 am

great tutorial. This is exactly what I needed to get back to cocos2D. I stopped developing for cocos2d in the 0.9.8 era and when i started again after all this time it left me confused. Thanks for this tutorial.
mojo
Baby Hacker
 
Posts: 6
Joined: Tue Jun 14, 2011 6:40 am
Has thanked: 2 times
Been thanked: 0 time

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby World of A » Wed Nov 21, 2012 8:34 am

I disagree flagging on a per-source-file basis with -fno-objc-arc. It is a way to do it, but it will be pretty hard to maintain your code when you update Cocos2d! Instead take a look at another approach : http://www.learn-cocos2d.com/2012/04/enabling-arc-cocos2d-project-howto-stepbystep-tutorialguide/

Here's a quote from Steffen in that tut:
Warning: Some recommend to use the -fno-objc-arc Compiler Flag to disable ARC on a per-source-file basis. This is only helpful if you have very few source code files which require this flag where an extra static library target would be overkill. Since you’ll have to add the flag to each cocos2d source code file individually – one-by-one – and there being anywhere between 100 and 150 source files, I strongly discourage going down this road. Plus it will be a maintenance nightmare whenever you upgrade cocos2d.
Image
World of A
[just fly]
http://www.worldofathesite.com
User avatar
World of A
Hacker
 
Posts: 17
Joined: Thu Apr 05, 2012 3:05 pm
Has thanked: 3 times
Been thanked: 5 times

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby rwenderlich » Wed Nov 21, 2012 2:03 pm

@World of A: Heh, and I disagree with that :]

Code: Select all
Since you’ll have to add the flag to each cocos2d source code file individually – one-by-one – and there being anywhere between 100 and 150 source files, I strongly discourage going down this road.


That is actually untrue - if you select the files and hit "Enter" in Xcode you can set all of them at once.

So upgrading to a new version of Cocos2D is no problem, since setting that flag on the files only takes < 10 seconds. :]

I'm not saying the library approach is bad - there are some nice things about doing it that way - but this way has the advantage of being nice and easy.
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2230
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 366 times

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby Pianisimo » Tue Feb 19, 2013 2:28 pm

I just updated cocos to latest version cocos2d-iphone-2.1-rc0a, followed all the steps, seems working fine but there is a warning in appDelegate.m which worries me.

inside the method:
Code: Select all
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   [director_ setDelegate:navController_];
}

the warning says:
sending 'UINAvigationController *_strong to parameter of incompatible type 'id<CCDirectorDelagete>'

Is this something I should ignore, or should I do anything else.
Pianisimo
Baby Hacker
 
Posts: 5
Joined: Tue Feb 19, 2013 2:19 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby sir_seagill » Mon Apr 22, 2013 12:28 pm

I tried the static library approach... ran into errors. this method worked for me, so yes this is easier :)
Mark W - A causal Flash designer now learning cocos2d (let the fun begin ^_^)
User avatar
sir_seagill
Hacker
 
Posts: 15
Joined: Sat Dec 25, 2010 10:57 am
Has thanked: 1 time
Been thanked: 0 time

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby RHerrema » Wed May 08, 2013 9:42 am

I've followed all the steps you've suggested, but when I build I get an error message: 'Expected ';' at end of declaration list' for this line in the AppDelegate.h file:

CCDirectorIOS *_unsafe_unretained director_; // weak ref

which seems strange, since the ';' is there. I'm using Xcode 4.5.2.
RHerrema
n00b
 
Posts: 2
Joined: Wed Feb 06, 2013 2:52 pm
Has thanked: 0 time
Been thanked: 0 time

Postby Manbearpig » Sun Sep 08, 2013 2:06 am

Wow. I've been all over the internet in search of a solution to all my memory management problems. This conversion to ARC pretty much solved them all. Thank you so much for this tutorial!
Manbearpig
n00b
 
Posts: 1
Joined: Sun Sep 08, 2013 2:03 am
Has thanked: 0 time
Been thanked: 0 time

Re: How To Enable ARC in a Cocos2D 2.X Project

Postby J-Crew » Sun Oct 27, 2013 8:25 pm

Hello all! Hopefully someone can help me with this problem.

In my current game using Cocos2D 2.1 with ARC, I have a MapScene that initializes a TMX Map and is loading object groups and creating an Event object for each object found on the map. The game takes on elements from a top down 2D RPG. The Event class represents NPCs. This is all working perfectly. I reuse this MapScene when changing maps. So from MapScene I replace the scene with a new MapScene, inserting the new TMX map to load.
Code: Select all
-(id)initWithMap:(NSString*)aMap


Now my problem with memory comes in when I change the scene to Map1, then to Map2, and back to Map1. When reloading Map1 the memory jumps about 4MB everytime, while the textures and data are supposed to either be stored in Cocos2D caches, or cleaned up on scene replacement. This memory jump also occurs when I am on MapScene:(Map1) and replace the scene with a new MapScene:(Map1). Am I wrong to believe that the reloading the same scene with the same map and objects should not increase the memory by 4MB each time, never dropping.

I have also tried changing to a new scene calling:
Code: Select all
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[[CCDirector sharedDirector] purgeCachedData];

and returning to the MapScene. Still the memory increases by about 4MB when returning to the MapScene. The below textures are removed. None of which seem to be the TMX tileset texture file.
Code: Select all
cocos2d: CCTextureCache: removing unused texture: Veda_Down_4.png
cocos2d: CCTextureCache: removing unused texture: NoButton.png
cocos2d: CCTextureCache: removing unused texture: Veda_Down_5.png
cocos2d: CCTextureCache: removing unused texture: MessageFont.png
cocos2d: CCTextureCache: removing unused texture: MessageArrow_1.png
cocos2d: CCTextureCache: removing unused texture: MessageCorner_1.png
cocos2d: CCTextureCache: removing unused texture: Veda_Up_4.png
cocos2d: CCTextureCache: removing unused texture: MessageVStrip_1.png
cocos2d: CCTextureCache: removing unused texture: MessageSquare_1.png
cocos2d: CCTextureCache: removing unused texture: MessageFill.png
cocos2d: CCTextureCache: removing unused texture: YesButton.png
cocos2d: CCTextureCache: removing unused texture: MessageHStrip_1.png
cocos2d: CCTextureCache: removing unused texture: WhitePixel.png
cocos2d: CCTextureCache: removing unused texture: Default.png
cocos2d: CCTextureCache: removing unused texture: BlackPixel.png


Below is the code I use to initialize the TMX map and Events:
Code: Select all
eventObjects = [[CCArray alloc] init];

// Init Nodes     
CCTMXTiledMap *map = [CCTMXTiledMap tiledMapWithTMXFile:aMap];
[self addChild:map z:1 tag:1];

// Initialize Event Objects
        CCTMXObjectGroup *npcMapObjects = [map objectGroupNamed:@"NPCs"];
        for (NSMutableDictionary *npc in npcMapObjects.objects) {           
            Event *npcObject = [[Event alloc] initWithEvents:npc map:map parent:self];
            [npcObject setScale:scaleFactor];
            int uniqueID = [[npc valueForKey:@"ID"] intValue];
            [self addChild:npcObject z:300 tag:100+uniqueID];
            [eventObjects addObject:npcObject];
        }


In the Event class, I allocate a few CCArrays and I add SpriteFrames to some CCAnimations.
I have tried setting these to nil in case ARC was not releasing them properly upon cleanup or they weren't being cleaned up by the parent.


Additional Information:
- I am Using XCode 5 with iOS7
- I am using an ARC setup from the following tutorial: http://www.raywenderlich.com/23854/
- I am Testing on an iPhone 4S device
- I have checked for leaks using Instruments

Below are screenshots of the Memory Jumps in XCode and Instruments:
Image
Image
Image
Image

As you can see the memory is Staircasing up. Each step occurs when I replace the scene with MapScene on the same or any map. In these tests, the TMX map and tileset are the same, yet the memory continues to grow. Only after about the tenth scene replacement, does a leak show up. In which case it seems that almost everything is leaking. Many leaks seem to be associated with TMX maps, CCSpriteFrames, and CCAnimations. I'm confused as I thought changing scenes would release memory for these objects automatically and ARC would handle the other objects that I specifically alocated.

Do I need to manually remove all objects from CCArrays and make pointers to CC-objects nil upon cleanup?

Any help would be greatly appreciated!
- Jaron Lowe
J-Crew
Hacker
 
Posts: 10
Joined: Mon Jul 30, 2012 6:54 pm
Has thanked: 0 time
Been thanked: 1 time


Return to Official Tutorials

Who is online

Users browsing this forum: Baidu [Spider] and 9 guests