在objective-c中release和dealloc的区别

时间:2022-07-13 21:30:32

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

当释放折射时,我已经看到使用了release和dealloc

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

  [super dealloc];
}

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

我的问题是什么时候使用发布,什么时候使用dealloc ?

Thanks

谢谢

3 个解决方案

#1


54  

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.

不要调用dealloc,除非在类的dealloc方法的末尾调用[super dealloc]。发布方法放弃对象的所有权。当一个Cocoa对象不再有任何所有者时,它可能被释放——在这种情况下,它将自动被发送一条dealloc消息。

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.

如果要编写Cocoa,需要阅读内存管理指南。一旦你克服了最初的困难,这是非常简单的,如果你不理解文档中的内容,你将会有很多微妙的错误。

#2


2  

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

当对象的retain count变为0时(通过发送发布消息的对象),将调用示例中的dealloc语句。

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

由于不再需要它,它通过向它所持有的对象发送发布消息来清理自己。

#3


1  

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.

在dealloc方法中,不应该显式地调用dealloc(除非它是超级dealloc),但这是唯一的例外。Objective-C通过引用计数来处理内存管理,所以你只需要将你的allocs/保留与release /autoreleases相匹配,让对象解构自己。

#1


54  

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.

不要调用dealloc,除非在类的dealloc方法的末尾调用[super dealloc]。发布方法放弃对象的所有权。当一个Cocoa对象不再有任何所有者时,它可能被释放——在这种情况下,它将自动被发送一条dealloc消息。

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.

如果要编写Cocoa,需要阅读内存管理指南。一旦你克服了最初的困难,这是非常简单的,如果你不理解文档中的内容,你将会有很多微妙的错误。

#2


2  

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

当对象的retain count变为0时(通过发送发布消息的对象),将调用示例中的dealloc语句。

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

由于不再需要它,它通过向它所持有的对象发送发布消息来清理自己。

#3


1  

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.

在dealloc方法中,不应该显式地调用dealloc(除非它是超级dealloc),但这是唯一的例外。Objective-C通过引用计数来处理内存管理,所以你只需要将你的allocs/保留与release /autoreleases相匹配,让对象解构自己。