does a default ios project use automatic reference counting or is it disabled ?
I believe disabled, but you can check following the info from SO.
(answer copied here)
Select your project, then Build Settings. Look for Objective-C Automatic Reference Counting in the Apple LLVM Compiler - Language section. Make sure you select the target; while you can set this in the project, the target can override it.
(You can also use the search bar in Build Settings for
Keep in mind, too, that you can turn ARC on or off on a per file basis in build phases.
Or, just try something like this in code:
[[[NSObject alloc] init] autorelease]
If you get an error:
ARC forbids explicit message send of 'autorelease'
Then you’re using ARC.
You can also require ARC from a source code file by checking for it:
#if !__has_feature(objc_arc) #error This file must be built with ARC. // You can turn on ARC for only this file by adding -fobjc-arc to the build phase. #endif
Do you know if cocos2d-x supports the option to turn on ARC, ? I wonder what the reason is why it’s not enabled by default like normal iOS projects
Probably an artifact of time, as most things cocos2d-x (cpp)
It likely just started that way and since there’s very few lines of Objective-C code relative the the entire engine it was deemed best to manage memory manually. This also matches the cocos2d-x memory model with its retain/release/autorelease.
(granted this is somewhat a guess)
You should be able to enable it without issue if you plan to write a significant amount of ObjC code for your specific project(s). My guess is that it would just warn on lines that could be removed. Oh, I suppose you might get a few
__bridge issues, but maybe not. Also, you can always compile the library itself without ARC and just enable it in your game project.
If you have a specific question, ask away. If it’s only a curiosity then just don’t worry about it.
when you mention “the cocos2d-x memory model with its retain/release/autorelease” I thought we no longer have to retain or release objects. how do we compile the library itself without ARC then enable it in my game project ? i thought they both compile together when you change the ARC setting in build settings ?
Where did you read that? It should be corrected.
In this book(pg 25) it says about “Ref” keeping the reference count which will be used to determine whether an object should be deleted from memory or not, maybe I interpreted it wrong and its just for Ref derived objects
I see. I always specify how I plan to work with the memory or an object. Manually works good for me. I have that book on my shelf so I will go pull it and read that section. Thank you.
makes sense ,i think I myself needs to re read that section to get a better understanding
If you use
::create() and always add the created node as a child of the scene or another node in the scene then all the retain/release/autorelease is effectively handled for you behind the scenes in the engine code.
As for mixing ARC, your tentativeness is a good thing. I would definitely recommend choosing one of the other, and if it is disabled by default I would just work with your game without using ARC.
If you’re trying to incorporate a lot of other ObjC (ios/mac specific) code that requires ARC be enabled then you should probably find another forum for those questions and/or read up on how it works. All ARC is doing for you is adding
release] and sometimes
retain] (with strong properties) in obvious code locations where the compiler can determine that you needed one or the other.
I dont think it would be considered “mixing ARC” if im just using it for some Objective-C librarys im using in my rootviewcontroller and appcontroller files in the iOS folder? im still letting cocos2d-x do the memory management for my c++ code behind the scenes, Enabling ARC will not have any effect of on my C++ code correct it only manages the objective-C portion ?
just seen this(https://github.com/cocos2d/cocos2d-x/pull/13937) i think its saying you can enable arc , and it won’t affect engine code