NSAutoreleasePool.什么时候适合创建一个新的自动释放池? [英] NSAutoreleasePool. When is it appropriate to create a new autorelease pool?
问题描述
在iOS/CocoaTouch上,我经常看到在一个方法中创建NSAutoreleasePool新实例的代码.我最近在NSOperation中看到了一个.
On iOS/CocoaTouch I often see code that creates a new instance of NSAutoreleasePool within a method. I recently saw one within an NSOperation.
设置新的NSAutoreleasePool实例的基本规则是什么?为什么比仅依赖main.m中创建的现有发布池更好呢?
What are the ground rules for setting up a new instance of NSAutoreleasePool? Why is this preferable to simply relying on the pre-existing release pool created in main.m?
谢谢,
道格
推荐答案
您可以随时使用新的自动释放池,但这并不总是有益的.每当您启动一个新线程,否则该线程中自动释放的对象将被泄漏时,这是必需的.在创建和自动释放大量对象的方法中,创建新的自动释放池也很常见.例如,如果您有一个循环,该循环在50个迭代中的每个循环中创建了10个对象,则应考虑为该方法创建一个自动释放池(如果不属于循环的一部分),则为每个迭代创建一个新的释放池.
You can use a new autorelease pool whenever you want, but it is not always beneficial. It is required whenever you start a new thread or objects autoreleased in that thread will be leaked. It is also common to create new autorelease pools in a method where you create and autorelease a large number of objects. For example, if you had a loop which created 10 objects in each of 50 iterations, you should consider creating a autorelease pool for that method, if not as part of the loop so that a new one is created for each iteration.
这篇关于NSAutoreleasePool.什么时候适合创建一个新的自动释放池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!