Difference between release and dealloc in objective-c

转载自:http://stackoverflow.com/questions/559295/difference-between-release-and-dealloc-in-objective-c

When deallocing a refrence I've seen release and dealloc being used for example

-(void)dealloc
{
  [foo release];
  [nar dealloc];

  [super dealloc];
}

My question is when is release to be used and when is dealloc to be used?

Thanks

link|improve this question

 
If you call [self dealloc] inside the -dealloc definition, then it will result in a recursion. Are you sure the code is correct? – codelogic Feb 18 '09 at 0:36


4 Answers

up vote25down voteaccepted

Never call dealloc except as [super dealloc] at the end of your class's dealloc method. Therelease method relinquishes ownership of an object. When a Cocoa object no longer has any owners, it may be deallocated — in which case it will automatically be sent a dealloc message.

If you're going to program Cocoa, you need to read the Memory Management Guidelines. It's incredibly simple once you get over the initial hump, and if you don't understand what's in that document, you'll have lots of subtle bugs.

link|improve this answer
 
what about [self dealloc] as I've shown in the example, is there really a need for that? – hhafez Feb 18 '09 at 0:28
2  
Not only is it unnecessary, calling [self dealloc] in your dealloc method creates an infinite loop. – Chuck Feb 18 '09 at 0:37


For the comment of accepted answer, it is not [self dealloc] it is [super dealloc] and he had meant to write [super dealloc] into your overrided dealloc mathod...

link|improve this answer
 
Welcome to stackoverflow. I have a little hint for you: If you don't like to be downvoted you should post comments as comments and not as answers. Just removed my finger from the downvote button, because I thought you shouldn't be downvoted on your first day. Have fun on SO. – Matthias Bauch Dec 26 '10 at 11:11
6  
You can't comment on stuff when you only have 3 rep. – MoominTroll Feb 13 '11 at 13:37
 
I wonder if this is still true with ARC. If you use ARC you can never call dealloc directly, not even [super dealloc] in an overridden dealloc routine so this code is no longer correct. Actually, can you even override dealloc if you use ARC? If not, where do you clean up? – Mike Jan 10 at 19:50
 
@Mike Yes, you can override dealloc in ARC. You use it to clean up resources that are not managed by ARC, such as Core Foundation objects. Also - it's a useful place to remove observers etc. – Abizern Jan 17 at 11:49



The dealloc statement in your example is called when the object's retain count becomes zero (through an object sending it a release message).

As it is no longer needed, it cleans itself up by sending a release message to the objects that it is holding on to.

link|improve this answer
   


You're never supposed to call dealloc explicitly (unless it's [super dealloc] within the dealloc method, but that's the only exception). Objective-C handles memory management via reference counting, so you're simply supposed to match your allocs/retains with releases/autoreleases and let the object deconstruct itself.

link|improve this answer

posted on 2012-02-17 02:34  yang3wei  阅读(246)  评论(0编辑  收藏  举报