您的位置:首页 > 移动开发 > IOS开发

NSAutoReleasePool文档摘抄

2014-04-14 10:49 381 查看
自动释放池永远只能在与创建时所在的上下文相同的上下文中被drain,这里所说的上下文指的是一个函数中或者一个run loop体中。

自动释放池只能“内联”式使用。我们永远没有任何理由将一个自动释放池的实例作为一个类的成员变量。

如果你释放了一个不在栈顶的自动释放池,这会导致栈中位于这个池之上的所有未被释放的自动释放池被释放。

另外:

If you neglect to send release to an autorelease pool when you are finished with it (something not recommended), it is released when one of the autorelease pools in which it nests is released.

This behavior has implications for exceptional conditions. If an exception occurs, and the thread suddenly transfers out of the current context, the pool associated with that context is released. However, if that pool is not the top pool on the thread’s
stack, all the pools above the released pool are also released (releasing all their objects in the process). The top autorelease pool on the thread’s stack then becomes the pool previously underneath the released pool associated with the exceptional condition.
Because of this behavior, exception handlers do not need to release objects that were sent autorelease. Neither is it necessary or even desirable for an exception handler to send release to its autorelease pool, unless the handler is re-raising the exception.

 

在Non-AppKit Programs中,简单的创建autoreleasepool即可

如果使用POSIX thread APIs来创建线程,那么我们不可以使用cocoa(包括autoreleasepool),除非cocoa处于多线程模式,开启cocoa的多线程模式,必须至少创建一个nsthread,所以,我们可以创建一个nsthread然后立即exit它。我们可以使用nsthread类的isMultiThreaded函数来检测cocoa是否处于多线程模式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios