Hello,
I'm thinking of writing an app for the iphone. But I'm confused in regards to the objective-c language. Am I correct in saying that I have to do my own memory management? Is Objective-C a managed language such as C# and/or Java?
Thank you!
Hello,
I'm thinking of writing an app for the iphone. But I'm confused in regards to the objective-c language. Am I correct in saying that I have to do my own memory management? Is Objective-C a managed language such as C# and/or Java?
Thank you!
You have to do your own memory management with Objective-C on the iPhone.
The answer isn't specific to Objective-C. If you're doing iPhone development, no...there is no garbage collector. If you're developing for Mac OSX, however, there is garbage collection.
As others have pointed out Objective-C has no garbage collector on the iPhone, but it does have one for Mac OSX. Here is an article describing this in more detail: http://vasudevkamath.blogspot.com/2010/01/objective-c-my-opinions.html
There is no garbage collection feature. However, any object you copy
, retain
, alloc
etc. has its retain count bumped up by one, and you are responsible for calling a release
or an autorelease
- you own an instance of that object. If you don't call [<YOUROBJECT> release];
or [<YOUROBJECT> autorelease];
, the object remains in the iPhone's/iPod Touch's/iPad's memory, but the pointer no longer remains, and a memory leak occurs, as that bit of memory can no longer be accessed. Autoreleasing adds the object to the autorelease pool, and means that it will become released at some point in the future. Always use a standard release
where possible, as autorelease
means that it will still stay in the device's memory for a while, you can never be sure when it will be released, and it is a slightly more demanding method to call.
Never call dealloc
on an object - releasing or autoreleasing is sufficient, and if required, the object will automatically be dealloced.
Make sure you never release objects you do not own, and if you do release objects, you release them after you no longer have any use of them - otherwise, you may try to access that object, and the device cannot find the object, and the situation results in your app crashing due to an EXEC_BAC_ACCESS
error - to find out the root of the problem, open Instruments, add the Object Allocation instrument and enable NSZombie detection in its preferences (or add the Zombies instrument (only available in iPhone SDK 3.2+)). You can then view the entire history of the object which causes the problem, and jump to the exact line of code that caused the problem.
You can read more here.
Hope this helped