November 2008
Monthly Archive
… projects, thoughts, etc.
Monthly Archive
Posted by Mathew Eis on 12 Nov 2008 | Tagged as: Others
Regardless of what your language background is, memory management in Objective-C can be a bit difficult to get a handle on. I won’t claim to be an expert on the topic, since I’m still learning myself, but here’s a few of the things I’ve learned while trying to unravel the complexity lately:
There’s a seeming mass of various methods with which you can message NSObjects and any class object that is extend from NSObject, including: alloc, dealloc, retain, release, autorelease, and finalize. But just what do all these mean?
First of all, you need to know that Objective-C has garbage collection capability. If you don’t know what garbage collection is, do a Google Search and familiarize yourself with the topic. Once you’re familiar with the wonders of garbage collection, you need to know that Objective-C may not always use it…
So, as best as I can tell, here’s what the above methods do:
That’s the ugly… Is general, things are actually quite simple:
1) If an object is created using alloc, it will have a retain count of 1, and should be released using release when the object is no longer needed.
2) Calling retain will increment the retain counter; release will need to be called the same number of times to bring the retain count to pre-retain state. (Excluding autorelease scenarios)