IPhone SDK: Dealloc vs. Release?

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

@property (save) ....

I use dealloc, but after reading this article I am not sure if this is correct.

You get ownership of the 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 to save the message. You are responsible for refraining from owning your own property through release or auto-advertising. At any other time, when you receive an object, you should not let it go.

+6
iphone
source share
3 answers

You should not call dealloc on anything other than super .

The only time you call dealloc is the dealloc method of the user inherited object, and it will be [super dealloc] .

When the object’s retention counter drops to zero, dealloc will be called automatically, so to work correctly with memory, you need to call retain and release , if necessary.

If this is not clear to you, or if you want information on how memory is managed in Cocoa, you should read the Memory Management Guide .

+21
source share

You never call dealloc directly. It is called by the system when the saveCount of the object goes to 0. Each time you perform a save, saveCount is incremented by 1. Each time you make a release, it decreases. This way, by balancing your save and release, you guarantee that when the keepCount value reaches 0, dealloc will be automatically called and your object will be freed.

As Ben C noted, the only time and place that you would call dealloc is in the dealloc method of the inherited object.

+5
source share

When you use @property(retain) and then @synthesize to generate the property code, you do not need to do any manual memory management in the property. Other answers are correct in that you should not use dealloc , except for your own override of the dealloc parent class.

0
source share

All Articles