Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is the dealloc method called?

When exactly is the dealloc method called? I've found that (in a lot of examples) much of the NS variables are released in the method it's instantiated in, but when synthesizing a component they place the release in the dealloc method.

like image 207
locoboy Avatar asked Apr 12 '11 05:04

locoboy


1 Answers

The Apple reference document clearly states Subsequent messages to the receiver may generate an error indicating that a message was sent to a deallocated object (provided the deallocated memory hasn’t been reused yet).

You never send a dealloc message directly. Instead, an object’s dealloc method is invoked indirectly through the release NSObject protocol method (if the release message results in the receiver's retain count becoming 0). See Memory Management Programming Guide for more details on the use of these methods.

Subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super:

Important: Note that when an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods. For this and other reasons, you should not manage scarce resources in dealloc

Another SO question iPhone - when is dealloc for a viewcontroller called?

like image 137
visakh7 Avatar answered Oct 16 '22 06:10

visakh7