Who calls the dealloc method and when in Objective C?
Asked Answered
B

4

15

When a custom class is created in Objective C, when and how is the dealloc method called? Is it something that I have to implement somehow in my class?

Bolyard answered 17/8, 2011 at 12:53 Comment(0)
D
19

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

 - (void)release
 {
   _retainCount--;
   if (_retainCount == 0) {
       [self dealloc];
    }
  }
Decomposed answered 17/8, 2011 at 12:58 Comment(0)
S
5

Imagine that -release is implemented in NSObject like this:

- (void)release
{
    _retainCount--;
    if (_retainCount == 0) {
        [self dealloc]
    }
}

I'm sure it's a little more complicated than that, but the answer to your question is that the object itself will call -dealloc when its retain count drops to zero. However, your custom class will inherit this behavior from NSObject. You'll never need to call -dealloc yourself in code that you write; it'll always happen automatically when the object has been properly released.

Styrene answered 17/8, 2011 at 13:0 Comment(3)
I hope this method body should be under 'release', rather than 'retain'Rainwater
@saran, you're absolutely right. My silly mistake; thanks for catching it.Styrene
really nice answer.i give an answer above but i think your answer is best in practical manner..Decomposed
L
2

dealloc is called as a result of memory management. Once an objects "retainCount" reaches 0 then a dealloc message is automatically sent to that object.

You should never call dealloc on objects unless it is a call to [super dealloc]; at the end of an overridden dealloc.

-(void)dealloc
{
    [ivar release]; //Release any retained variables before super dealloc

    [super dealloc]; //Only place in your code you should ever call dealloc
}

And according to the -[NSObject dealloc] discussion

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.

Laquitalar answered 17/8, 2011 at 12:59 Comment(0)
B
1

The runtime will do that for you when the object is no longer needed (which the runtime decides as well). Just make sure you retain and release properly. Do not call dealloc on other objects.

Briones answered 17/8, 2011 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.