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

发布于 2024-08-04 20:57:07 字数 218 浏览 5 评论 0原文

当您在 Objective-C 中释放一个对象时(假设其释放计数为 1),其释放计数将递减至 0,并调用 dealloc 方法。对象是在[super dealloc]之后立即销毁,还是添加到池中并在池耗尽时销毁?

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

干杯-加里-

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?

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.

cheers -gary-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2024-08-11 20:57:07

首先,Objective-C 这种编程语言没有任何内存管理的概念。内存管理内置于 Foundation(Mac OS X 上的 Cocoa 和 iPhone OS 上的 Cocoa Touch 的通用框架)。 Foundation 添加了一个根类 NSObject,它实现了 allocretainreleaseautorelease 作为 class_createInstance()object_dispose() 函数。

由于 Objective-C 与内存管理无关,因此添加垃圾收集并使 NSObject 上的所有内存管理方法无操作非常容易。但 iPhone OS 和旧版 Mac OS X 上没有垃圾收集,我们在 Cocoa 中使用引用计数方案。

当从 Foundation 调用 NSObjectNSProxy 上的 alloc 类方法时,会创建一个对象。这些默认实现将调用 class_createInstance(),因此您无需手动执行。

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

就运行时而言,自动释放的对象不会受到任何特殊对待,自动释放对象与任何其他对象一样活跃。当您自动释放一个对象时,大约会发生什么;

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

调用 autorelease 不会减少保留计数,它只会将对象的所有权从调用者转移到当前的自动释放池。稍后,当当前自动释放池消失时,它将对其拥有的所有对象调用release,并且不再由其他任何对象拥有的任何对象都会被释放。

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.

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.

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.

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.

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;

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

热情消退 2024-08-11 20:57:07

是的,一旦保留计数为零,它们就会被释放。

自动释放系统适用于所有权有点“模糊”的对象 - 即,当您返回一个您不想拥有的新对象时,其生命周期未知,并且您不想假设调用者会占用该对象的责任。当自动释放池耗尽时(通常是下一次运行循环),所有成员都会被发送释放。如果接收对象的调用者想要对其负责,它所需要做的就是保留它,这将避免其释放。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文