Difference between release and dealloc in objective-c

Difference between release and dealloc in objective-c,第1张

概述转载自:http://stackoverflow.com/questions/559295/difference-between-release-and-dealloc-in-objective-c 17 down vote favorite 9 share [g+] share [fb] share [tw] When deallocing a refrence I've seen releas

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

17 down vote favorite 9 share [g+] share [fb] share [tw]

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

-(voID)dealloc {   [foo release];nar deallocsuper dealloc } 

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

Thanks

objective-c  memory
link | improve this question edited  Feb 18 '09 at 1:29
asked  Feb 18 '09 at 0:03

hhafez
7,920 8 40 82
98% accept rate
 
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 answered  Feb 18 '09 at 0:11

Chuck
75.3k 6 65 163
 
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


3 down vote

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...

| improve this answer answered  Dec 26 '10 at 11:03

Dogan Coruh
31 1
 
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



2 down vote

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.

| improve this answer answered  Feb 20 '09 at 10:22

Abizern
20.7k 3 30 76
   


1 down vote

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.

| improve this answer answered  Aug 25 '11 at 11:02

Vadoff
182 1 8
总结

以上是内存溢出为你收集整理的Difference between release and dealloc in objective-c全部内容,希望文章能够帮你解决Difference between release and dealloc in objective-c所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1062596.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存