什么时候自动释放实际上导致Cocoa Touch中的释放? [英] When does autorelease actually cause a release in Cocoa Touch?

查看:157
本文介绍了什么时候自动释放实际上导致Cocoa Touch中的释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您需要小心在iOS上的 autorelease 。我有一个方法,返回一个对象 alloc s是调用者所需要的,所以在这种情况下 - 根据我的理解 - 我需要发送<$

I understand you need to be careful with autorelease on iOS. I have a method that is returning an object it allocs which is needed by the caller, so in this situation -- as I understand it -- I need to send autorelease to the object in the callee before it returns.

这很好,但是一旦控制返回到电话(例如:c $ c> autorelease 之后我的按钮点击已经处理)似乎自动释放池被释放。我怀疑这是怎么回事,但我想知道什么是这种情况的最佳做法。

This is fine, but once control returns to the phone (i.e. after my button click has been processed) it seems that the autorelease pool is released. I suspect this is how it is supposed to be, but I am wondering what is the best practice for this situation.

我已经诉诸发送 retain 消息,使对象不被释放,然后在 dealloc 中显式释放它。

I have resorted to sending a retain message from the caller so that the object is not released and then explicitly releasing it in dealloc.

这是最好的方法吗?

推荐答案

自动释放池通常在运行循环。大致来说,每个Cocoa和Cocoa Touch应用程序的结构如下:

The autorelease pool is typically released after each iteration of the run loop. Roughly, every Cocoa and Cocoa Touch application is structured like this:

Get the next message out of the queue
Create an autorelease pool
Dispatch the message (this is where your application does its work)
Drain the autorelease pool

你描述的是预期的行为。如果你想保持一个对象周围的任何更长的时间,你需要明确保留它。

What you describe is the expected behavior. If you want to keep an object around any longer than that, you'll need to explicitly retain it.

这篇关于什么时候自动释放实际上导致Cocoa Touch中的释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆