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 vote 25 down vote accepted

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值