Jan 11, 2010

Mac Dev Center: Memory Management Programming Guide for Cocoa: Memory Management Rules

Just a friendly little reminder of the memory management rules in Objective-C, since they seem to always cause so much confusion, despite being so simple:

This is the fundamental rule:

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, allocnewObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

It really does surprise me that even with rules this simple, there are still developers that throw up their hands in the air and cry out for garbage collection.  If you think this is difficult, go try managing memory in C or C++.

Comments
blog comments powered by Disqus
About