NSAutoreleasePool. When is it appropriate to create a new autorelease pool?

孤人 提交于 2019-12-04 08:36:26

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.

Create your own pool when there isn't already one in place (such as in a new thread), or when the one in the run loop isn't sufficient (creating autoreleased objects in a loop that will run for many iterations), or when you want increased control over when the autoreleased objects you create are ultimately released.

I tested in iOS 4.3 and you need to create own autorelease pool when execute performSelectorInBackground. You do not need to create when using NSOperation or dispatch_async.

Seems in iOS >= 5.0 the system creates autorelease pool automatically even if use performSelectorInBackground, so I was unable to find a case when you need to create own autorelease pool.

Was unable to find that change documented, though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!