Objective-C中Release和Dealloc之间的区别 [英] Difference between release and dealloc in objective-c
问题描述
取消分配引用时,我已经看到例如使用release
和dealloc
When deallocing a refrence I've seen release
and dealloc
being used for example
-(void)dealloc
{
[foo release];
[nar dealloc];
[super dealloc];
}
我的问题是何时使用release
,何时使用dealloc
?
My question is when is release
to be used and when is dealloc
to be used?
谢谢
推荐答案
在类的dealloc
方法末尾,除非以[super dealloc]
的形式调用,否则请勿调用dealloc
. release
方法放弃对象的所有权.当可可对象不再拥有任何所有者时,可以将其释放-在这种情况下,将自动向其发送dealloc
消息.
Never call dealloc
except as [super dealloc]
at the end of your class's dealloc
method. The release
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.
这篇关于Objective-C中Release和Dealloc之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!