cocoa-touch - 什么时候自动释放实际上会导致 Cocoa Touch 中的释放?

标签 cocoa-touch memory-management autorelease nsautoreleasepool

我知道您需要小心 autorelease在 iOS 上。我有一个方法返回一个对象它 alloc s 这是调用者需要的,所以在这种情况下——据我所知——我需要发送 autorelease到被调用者中的对象,然后再返回。

这很好,但是一旦控制权返回到手机(即在我的按钮点击处理完成后),似乎自动释放池被释放了。我怀疑这是应该的,但我想知道这种情况的最佳做法是什么。

我已经求助于发送 retain来自调用者的消息,以便对象不会被释放,然后在 dealloc 中显式释放它.

这是最好的方法吗?

最佳答案

自动释放池通常在每次运行循环迭代后释放。粗略地说,每个 Cocoa 和 Cocoa Touch 应用程序的结构如下:

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

您所描述的是预期的行为。如果您想保留一个对象的时间比这更长,则需要明确保留它。

关于cocoa-touch - 什么时候自动释放实际上会导致 Cocoa Touch 中的释放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/673372/

相关文章:

ios - cocoa touch : Multiple line text input

macos - 在 Mac OS X 上的 64 位进程中,中等大小的内存分配如何失败?

algorithm - 用于与多个任意值进行比较的存储算法

objective-c - stringByAppendingString 和保留困境

ios - 如何在没有内存泄漏的情况下返回 CFDataRef?[ios]

ios - 自动释放的 NSArray 会导致内存泄漏吗?

iphone - 显示应用程序在后台运行的持续时间

iphone - 为什么程序员使用configureCell :atIndexPath: method to config the tableView Cell

iphone - 设置 bundle 的自定义 View Controller ?

c++ - 将多个内存块呈现为单个连续内存块的容器