运行循环结束 - 自动释放池恢复 [英] End of run loop -- autorelease pool recovery

查看:88
本文介绍了运行循环结束 - 自动释放池恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,自动释放的对象会在自动释放的池释放后清除。现在,autorelease pool将在运行循环结束时释放。



我的问题是,如果在我的类中,我不是创建一个自定义autorelease池和调用autorelease方法对该类中的某些对象,在什么时候这些对象将被恢复? 运行循环结束是指应用结束吗?

解决方案

运行循环。 iOS中的运行循环等待一些事件发生,然后它对它进行操作。该事件可以是用户触摸屏幕,接收呼叫等。



对于iOS处理的每个此类事件,都会有一个新的自动释放池在开始时创建,并在事件处理完成时耗尽。理论上,可以有任意数量的嵌套自动释放池由Cocoa Touch创建,但你应该知道的主要是事件循环。



也许这个图从应用程序生命周期将会有所帮助。 / p>



在伪代码中,这可以归结为

  int UIApplicationMain {
while(!shouldQuitApplication){
Event * someEvent = //等待下一个事件;
NSAutoreleasePool * myPool = [[NSAutoreleasePool alloc] init];
// handle event
[myPool release];
}
}

这些是iOS中的事件类型

  UIEventTypeTouches,
UIEventTypeMotion,
UIEventTypeRemoteControl,

因此,在每次触摸,移动或遥控器事件被处理后,池将被耗尽。


As I understand, autoreleased objects are cleaned once an autoreleased pool is released. Now, autorelease pool will be released at the end of the run loop.

My question is, if in my class I am not creating a custom autorelease pool and calling the autorelease method on some objects in that class, at what point will those objects be recovered? Is the "end of the run loop" imply the "end of application"?

解决方案

You have to understand the concept of a run-loop. The run loop in iOS waits for some event to happen and then it acts upon it. That event could be the user touching the screen, receiving a call, etc.

For every such event that iOS handles, a new autorelease pool is created at the beginning and drained when the event processing is complete. Theoretically there could be any number of nested autorelease pools created by Cocoa Touch, but the main one you should know about is the event loop.

Maybe this diagram from the Application Life Cycle will help.

.

In pseudo-code, this boils down to,

int UIApplicationMain(...) {
    while (!shouldQuitApplication) {
        Event *someEvent = // wait for next event;
        NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
        // handle event
        [myPool release];
    }
}

These are the event types in iOS

UIEventTypeTouches,
UIEventTypeMotion,
UIEventTypeRemoteControl,

So after every touch, motion, or remote control event is processed, the pool will be drained.

这篇关于运行循环结束 - 自动释放池恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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