【发布时间】:2013-04-18 09:17:21
【问题描述】:
当我读到自动释放池块时,我正在阅读来自苹果的关于内存管理的文档,这让我开始思考。
Any object sent an autorelease message inside the autorelease pool block is
released at the end of the block.
我不确定我是否完全理解这一点。无论如何,在自动释放池块内创建的任何对象都会在块的末尾释放,因为那是它的生命周期。当对象到达块的末尾时无论如何都将被释放,为什么你需要调用自动释放对象?
为了更清楚,我将举一个例子,说明我的想法:
@autoreleasepool {
MyObject *obj = [[MyObject alloc] init]; // no autorelease call here
/* use the object*/
//....
// in the end it should get deallocated because it's lifespan ends, right?
// so why do we need to call autorelease then?!
}
PS:请不要告诉我,因为 ARC,我们不需要做一些事情,因为 ARC 会照顾它们。我完全清楚这一点,但我想暂时搁置 ARC 以了解内存管理的机制。
【问题讨论】:
标签: ios memory-management objective-c-blocks autorelease nsautoreleasepool