需要澄清NSAutoreleasePool [英] Need clarification for NSAutoreleasePool

查看:77
本文介绍了需要澄清NSAutoreleasePool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我们调用autorelease方法时,其对象都将移至NSAutoreleasePool.当池耗尽时,它将向池中的所有对象发送释放.

Whenever we are calling autorelease method, its object is going to NSAutoreleasePool. When the pool is drained, it is sending release to all the objects in the pool.

我的问题是;
在主要功能中,有一个NSAutoreleasePool.我想知道当我们调用autorelease方法时,它将对象发送到哪里?我是说;它会将对象发送到主功能(或)中某处的NSAutoreleasePool吗?

My question is;
In the main function there is one NSAutoreleasePool. I want to know that; when we call the autorelease method, where it is sending the object ? I mean; it is sending the object to NSAutoreleasePool which is in main function (or) somewhere ?

谢谢.

推荐答案

实际上有一堆自动释放池.每当您执行[[NSAutoreleasePool alloc] init]时,新创建的池都会自动放在自动释放池堆栈的顶部.您可以随时创建自己的池.

There is actually a stack of autorelease pools. Whenever you do [[NSAutoreleasePool alloc] init] that newly created pool is automatically put on top of the autorelease pool stack. You can create your own pools whenever you need it.

更准确地说:每个线程上都有一堆自动释放池 .因此,无论何时创建线程(例如,使用[foo performSelectorInBackground:@selector(bar) withObject:baz]),首先要做的就是创建一个池,否则对象将泄漏(这会产生臭名昭著的消息,例如"NSAutoreleaseNoPool():类的对象0xd819d0 NSCFString是在没有池的情况下自动释放的,只是泄漏" ,它是在此处的常见问题解答).

To be more precise: there is a stack of autorelease pools on each thread. So whenever you create a thread (for example with [foo performSelectorInBackground:@selector(bar) withObject:baz]) the very first thing you need to do is create a pool or else your objects leak (this creates the infamous messages like "NSAutoreleaseNoPool(): Object 0xd819d0 of class NSCFString autoreleased with no pool in place - just leaking" and is a very frequently asked question here on SO).

当您调用autorelease时,该对象将在当前线程的最顶层自动释放池中注册(即,该线程的最后一个自动释放池).主运行循环具有其自己的自动释放池,该池在每次运行循环迭代(AFAIK)时都会清空. main.m中的池在那里可以捕获任何对象,例如可能由Cocoa Touch内部创建的对象,然后才创建运行循环自动释放池.

When you call autorelease, the object is registered with the top-most autorelease pool of the current thread (that is: the one that was created last on that thread). The main run loop has its own autorelease pool that is emptied on each run loop iteration (AFAIK). The pool from main.m is there to catch any objects that for example might be generated internally by Cocoa Touch before it gets to create the run loop autorelease pool.

编辑:有关更多幕后信息,请参见

Edit: For more behind-the-scenes information, see Mike Ash's "Let's Build NSAutoreleasePool"

这篇关于需要澄清NSAutoreleasePool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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