什么时候释放对象最终被销毁?

时间:2021-11-25 09:39:41

When you release an object in Objective-C (assuming its release count is 1) its release count is decremented to 0 and the dealloc method called. Is the object destroyed right there and then after the [super dealloc], or is it added to the pool and destroyed when the pool is drained?

当您在Objective-C中释放一个对象时(假设它的发布计数是1),它的发布计数会递减到0,而dealloc方法调用。对象是在这里被销毁,然后在[super dealloc]之后被销毁,还是添加到池中并在池被耗尽时被销毁?

I would assume that released objects are destroyed at the end of dealloc (when [super dealloc] is called) I know autorelease variables are added to the pool, just wanted to be sure what happens to normal released objects.

我假设释放的对象在dealloc(调用[super dealloc]时)结束时被销毁,我知道自动释放变量被添加到池中,只是想确定正常释放的对象会发生什么。

cheers -gary-

欢呼声加里-

2 个解决方案

#1


8  

First off, Objective-C the programming language does not have any concept of memory management. Memory management is built into Foundation (the common framework of Cocoa for Mac OS X and Cocoa Touch on iPhone OS). Foundation adds a rootclass NSObject that implements alloc, retain, release and autorelease as convenience wrappers on top of class_createInstance() and object_dispose() functions from the Objective-C run-time.

首先,Objective-C编程语言没有任何内存管理的概念。内存管理内置在Foundation (Mac OS X的Cocoa通用框架和iPhone OS上的Cocoa Touch)中。Foundation在class_createInstance()和object_dispose()函数之上添加了一个rootclass NSObject,该对象实现alloc、retain、release和autorelease,作为Objective-C运行时的class_createInstance()和object_dispose()函数的便利包装器。

Since Objective-C is memory management agnostic, adding garbage collection and making all memory management methods on NSObject no-ops was quite easy. But there is no garbage collection on iPhone OS and legacy Mac OS X and there we instead use a reference counting scheme in Cocoa.

由于Objective-C与内存管理无关,在NSObject no-ops上添加垃圾收集并使所有内存管理方法变得非常简单。但是在iPhone OS和遗留的Mac OS X上没有垃圾收集,我们在Cocoa中使用一个引用计数机制。

An object is created when calling the alloc class method on NSObject or NSProxy from Foundation. These default implementations will call class_createInstance() so that you never need to manually.

当从Foundation调用NSObject或NSProxy上的alloc类方法时,将创建一个对象。这些默认实现将调用class_createInstance(),因此您不需要手动。

An object "dies" when dealloc is run on the root class NSObject. This is when the memory for the object on the heap is released by calling object_dispose(), you will never need to call this function yourself as long as you inherit from NSObject or NSProxy from Foundation.

当在根类NSObject上运行dealloc时,对象“死亡”。这是通过调用object_dispose()来释放堆上的对象的内存时,只要从Foundation继承自NSObject或NSProxy,就永远不需要调用这个函数。

Autoreleased objects are not treated any special as far as the run-time is concerned, an autoreleased object is just as alive as any other object. What happens when you autorelease an object is approximately;

就运行时而言,autoreleated对象没有得到任何特殊处理,autoreleated对象与任何其他对象一样是活动的。当你autorelease一个物体近似;

-(id)autorelease; {
  [NSAutoreleasePool addObject:self];  // retain count +1
  [self release];                      // retain count -1
  return self;
}

Calling autorelease will not decrement the retain count, it will just transfer ownership of the object from the caller to the current autorelease pool. Later when the current autorelease pool goes away, it will call release on all of the objects it owns, and any object no longer owned by anything else is released.

调用autorelease不会降低保留计数,它只会将对象的所有权从调用者转移到当前的autorelease池。稍后,当当前的autorelease池消失时,它将调用它所拥有的所有对象的release函数,并释放任何不再属于其他任何对象的对象。

#2


5  

Yes, they are deallocated as soon as the retain-count hits zero.

是的,一旦预订数量达到零,它们就会被释放。

The autorelease system is for objects who's ownership is a little "ambiguous" - i.e. when you're returning a fresh object that you don't want to own, whose lifetime is unknown, and you don't want to assume the caller will take responsibility for. When the autorelease pool is drained (generally next time around the run-loop), all members are sent release. If the caller who received your object wants to take responsibility for it, all it need do is retain it, and this will avoid its deallocation.

autorelease系统针对的是所有权有点“模糊”的对象——例如,当您返回一个您不想拥有的新对象时,它的生命周期是未知的,并且您不希望假定调用者将对此负责。当autorelease池被抽干(通常是在运行循环的下一次),所有成员都被释放。如果接收您的对象的调用者希望对其负责,那么它所需要做的就是保留它,这将避免它的重新分配。

#1


8  

First off, Objective-C the programming language does not have any concept of memory management. Memory management is built into Foundation (the common framework of Cocoa for Mac OS X and Cocoa Touch on iPhone OS). Foundation adds a rootclass NSObject that implements alloc, retain, release and autorelease as convenience wrappers on top of class_createInstance() and object_dispose() functions from the Objective-C run-time.

首先,Objective-C编程语言没有任何内存管理的概念。内存管理内置在Foundation (Mac OS X的Cocoa通用框架和iPhone OS上的Cocoa Touch)中。Foundation在class_createInstance()和object_dispose()函数之上添加了一个rootclass NSObject,该对象实现alloc、retain、release和autorelease,作为Objective-C运行时的class_createInstance()和object_dispose()函数的便利包装器。

Since Objective-C is memory management agnostic, adding garbage collection and making all memory management methods on NSObject no-ops was quite easy. But there is no garbage collection on iPhone OS and legacy Mac OS X and there we instead use a reference counting scheme in Cocoa.

由于Objective-C与内存管理无关,在NSObject no-ops上添加垃圾收集并使所有内存管理方法变得非常简单。但是在iPhone OS和遗留的Mac OS X上没有垃圾收集,我们在Cocoa中使用一个引用计数机制。

An object is created when calling the alloc class method on NSObject or NSProxy from Foundation. These default implementations will call class_createInstance() so that you never need to manually.

当从Foundation调用NSObject或NSProxy上的alloc类方法时,将创建一个对象。这些默认实现将调用class_createInstance(),因此您不需要手动。

An object "dies" when dealloc is run on the root class NSObject. This is when the memory for the object on the heap is released by calling object_dispose(), you will never need to call this function yourself as long as you inherit from NSObject or NSProxy from Foundation.

当在根类NSObject上运行dealloc时,对象“死亡”。这是通过调用object_dispose()来释放堆上的对象的内存时,只要从Foundation继承自NSObject或NSProxy,就永远不需要调用这个函数。

Autoreleased objects are not treated any special as far as the run-time is concerned, an autoreleased object is just as alive as any other object. What happens when you autorelease an object is approximately;

就运行时而言,autoreleated对象没有得到任何特殊处理,autoreleated对象与任何其他对象一样是活动的。当你autorelease一个物体近似;

-(id)autorelease; {
  [NSAutoreleasePool addObject:self];  // retain count +1
  [self release];                      // retain count -1
  return self;
}

Calling autorelease will not decrement the retain count, it will just transfer ownership of the object from the caller to the current autorelease pool. Later when the current autorelease pool goes away, it will call release on all of the objects it owns, and any object no longer owned by anything else is released.

调用autorelease不会降低保留计数,它只会将对象的所有权从调用者转移到当前的autorelease池。稍后,当当前的autorelease池消失时,它将调用它所拥有的所有对象的release函数,并释放任何不再属于其他任何对象的对象。

#2


5  

Yes, they are deallocated as soon as the retain-count hits zero.

是的,一旦预订数量达到零,它们就会被释放。

The autorelease system is for objects who's ownership is a little "ambiguous" - i.e. when you're returning a fresh object that you don't want to own, whose lifetime is unknown, and you don't want to assume the caller will take responsibility for. When the autorelease pool is drained (generally next time around the run-loop), all members are sent release. If the caller who received your object wants to take responsibility for it, all it need do is retain it, and this will avoid its deallocation.

autorelease系统针对的是所有权有点“模糊”的对象——例如,当您返回一个您不想拥有的新对象时,它的生命周期是未知的,并且您不希望假定调用者将对此负责。当autorelease池被抽干(通常是在运行循环的下一次),所有成员都被释放。如果接收您的对象的调用者希望对其负责,那么它所需要做的就是保留它,这将避免它的重新分配。