关于autoreleasepool

简单的说,每次事件处理时候开始的时候,ios会为我们自动生成一个autorelesepool,,结束的时候释放掉。

之前我们有误解是只有在main程序中有一个autoreleasepool,我们代码中所有autorelease对象都在这个pool中,这个观点是不正确的。

 

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.

<!--[if !vml]--><!--[endif]-->.

In pseudo-code, this boils down to,

intUIApplicationMain(...){
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
    while(!shouldQuitApplication){
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
        Event*someEvent =// wait for next event;
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
        NSAutoreleasePool*myPool =[[NSAutoreleasePool alloc] init];
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
        // handle event
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
        [myPool release];
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
    }
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
}
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->

<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
 

These are the event types in iOS

UIEventTypeTouches,
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
UIEventTypeMotion,
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
UIEventTypeRemoteControl,

<!--[if !supportLineBreakNewLine]--> 

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


by anerevol

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值