Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone SDK: Dealloc vs. Release?

Tags:

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.

like image 734
jp chance Avatar asked Nov 05 '09 18:11

jp chance


1 Answers

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.

like image 164
Ben S Avatar answered Oct 07 '22 16:10

Ben S