关于IPhone内存管理的澄清(自动释放)

时间:2010-11-23 19:30:28

标签: iphone memory-management autorelease

我知道在here之前已经回答了类似的问题,但我只是想确保我对它有所了解。这是我的情景...

我有一个辅助类方法,它返回一个已分配的UIImageView,如下所示。

+(UIImageView *)tableCellButton{
 return [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]] autorelease];

}

然后,在我的一个UIViewController方法中,我正在使用它..

UIImageView *imageView = [Helper tableCellButton];
imageView.frame = cell.backgroundView.bounds;
imageView.tag = 250;
[cell.backgroundView addSubview:imageView];

我的问题是如何释放这种记忆。我没有使用自动释放池(除了创建的应用程序之外),并且变量不是iVar / Property(因此在调用dealloc时不会释放它)。在这种情况下,我是否负责在我打电话后释放内存?什么时候自动释放发挥作用?谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

autorelease的调用将导致release在下次通过事件循环时被发送到对象。这将考虑您在alloc中进行的tableCellButton来电。保留对象的唯一另一个时间是addSubview内部,它还将处理同一对象的自己的release。根据上面的代码,您可以检查此对象的内存管理。

答案 1 :(得分:1)

runloop的每次迭代都有自己的自动释放池。

基本上,可以这样想:

while(1)
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [someObject doSomething];
    [pool drain];
}
相关问题