运行循环结束——自动释放池恢复

发布于 2024-11-03 10:57:29 字数 158 浏览 4 评论 0原文

据我了解,一旦自动释放池被释放,自动释放的对象就会被清理。现在,自动释放池将在运行循环结束时释放。

我的问题是,如果在我的类中我没有创建自定义自动释放池并对该类中的某些对象调用 autorelease 方法,那么这些对象在什么时候会被恢复? “运行循环结束”是否意味着“应用程序结束”?

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望笑 2024-11-10 10:57:29

您必须了解运行循环的概念。 iOS 中的运行循环等待某个事件发生,然后对其采取行动。该事件可能是用户触摸屏幕、接听电话等。

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

也许这个图表来自应用程序生命周期 会有帮助。

UIKit 事件循环

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

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

这些是 iOS 中的事件类型

UIEventTypeTouches,
UIEventTypeMotion,
UIEventTypeRemoteControl,

,因此在处理每个触摸、运动或远程控制事件后,池将被清空。

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.

UIKit event loop.

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.

往事随风而去 2024-11-10 10:57:29

运行循环的“结束”意味着运行循环每次迭代的结束,而不是应用程序的结束。

The "end" of the run loop means the end of each iteration of the run loop, not the end of the application.

戏蝶舞 2024-11-10 10:57:29

并不真地。想象一下 RunLoop 有“圈子”:) 在每个“圈子”的开始,RunLoop 创建自动释放池并在退出“圈子”之前耗尽它。

Not really. Imagine that RunLoop has "circles" :) In the begin of every "circle" RunLoop creates Autorelease pool and drains it before quitting the "circle".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文