cocoa内存autorelease pool原理

来源:互联网 发布:mac 重启后 壁纸 编辑:程序博客网 时间:2024/05/22 12:16

         苹果官方文档:The AppKit and UIKit frameworks automatically create a pool at the beginning of each event-loop iteration, such as a mouse down event or a tap, and drain it at the end.   AppKit和UIKit框架在每个event-loop事件循环中创建一个pool,如鼠标按下事件或者一个手势,这个pool会在这个事件结束时释放掉。


          即:当一个事件产生时(如鼠标事件、触摸屏事件、网络事件、定时器事件),系统会创建一个autorelease pool,系统会把本次事件处理中的autorelease对象放入到autorelease pool中,当本次事件处理结束时,系统会销毁这个autorelease pool,销毁这个autorelease pool时,会把池中对象的引用计数减一,如果有对象的引用计数为0时,则释放这个对象。当下一个事件到来时,系统会创建一个新的autorelease pool.