IPhone SDK: Dealloc vs. Release

iphone

I am wondering what the difference is between release and dealloc? After reading, the memory management rules (See below)., I am thinking most of the time I will be using release. However, I wanted to know what to do for properties.

@property(retain)….

I have been using dealloc but after reading this article I am not sure that is correct.

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, alloc, newObject, 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.

Best Answer

You should never call dealloc on anything other than super.

The only time you will be calling dealloc is in the dealloc method of a custom inherited object, and it will be [super dealloc].

When the retain count of an object goes down to zero, dealloc will automatically be called for you, so in order to manage memory properly, you need to call retain and release when appropriate.

If this isn't clear to you or you'd like details about how memory is managed in Cocoa, you should read the Memory Management Programing Guide.

Related Topic