ディfference between release and dealloc in objective-c

32166 ワード

自己転載:http://stackoverflow.com/questions/559295/difference-between-release-and-dealloc-in-objective-c
17
down vote
favorite
9
share[g+]
share[fb]

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
エディット
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 reult in a recursion.Aree you sure the code is corect?
codelogic
Feb 18'09 at 0:36
4 Answers
up vote
25
down vote
accepted
Never cal dealloc except a s [super dealloc] at the end of your clast's dealloc method.The release method relinishes owners abject.When a Cocoa ojectのlonger has any ners it,may decatech 142
If you're going to program Cocoa,you need to read the Memory Management Gridelines.It's incredbly simple once you get over the initial hmp,and if you don't understand what's in that document,you'll have lots of subtbugs.
link
𞓜
improve this answer
answered
Feb 18'09 at 0:11
Chuck
75.3 k
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
up vote
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…
link
𞓜
improve this answer
answered
Dec 26'10 at 11:03
ドガンコード
31
1
Welcome to stackoverflow.I have a litle hint for you:If you don't like to be downvoted you shout post comments and not as.Just removed my finger from the downvote button,berfutun Hadound.com。
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 cal dealloc directly,not even[super dealloc]n overridden dealloc routine so this code isのlonger corect.Actualy,caneverover.Actublic。If not,where do you clean up?–
マイク
Jan 10 at 19:50
@Mike Yes,you can override dealloc in ARC.You use it to clean up reso ources that are not manage d byARC,such a s Core Foundation objecs.Also-it's a useful place to remove overs etc.
Abizern
Jan 17 at 11:49
up vote
2
down vote
The dealloc statement in your example is caled when the object's retain count becocommes zero.
As it isのlonger need、it cleans itself up by sending a dealloc message to the object that it is holding on to.
link
𞓜
improve this answer
answered
Feb 20'09 at 10:22
Abizern
20.7 k
3
30
76
up vote
1
down vote
You're never supposed to call dealloc explicitly(unless it s's[super dealloc]within the dealloc method,but that's the only exception).Objecive--Candledndlememorymemomorymanarerereinininfefefeference coinininininininininststststststststrerererererererereaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaarerererererererererererererererererereaaaaaaaaaaaaaconstruct itself.
link
𞓜
improve this answer
answered
Aug 25'11 at 11:02
Vadoff
182
1
8