代码之家  ›  专栏  ›  技术社区  ›  DerekH

关于IPhone内存管理(自动释放)的说明

  •  1
  • DerekH  · 技术社区  · 14 年前

    我知道以前也有人回答过类似的问题 here ,但我只是想让自己更明白一点。这是我的设想。。。

    我有一个helper类方法,它返回一个已分配的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/属性(因此在调用dealloc时不会释放)。在这种情况下,我有责任在调用内存后释放它吗?自动释放什么时候起作用?谢谢你的帮助!

    2 回复  |  直到 7 年前
        1
  •  1
  •   fbrereto    14 年前

    A call to autorelease 会导致 release 下次通过事件循环发送到对象。这将解释 alloc 叫你进来 tableCellButton . 唯一保留对象的其他时间是在 addSubview ,它也将处理自己的 释放 同一物体的。然后,根据上面的代码,您将签出此对象的内存管理。

        2
  •  1
  •   David Liu    14 年前

    运行循环的每个迭代都有自己的自动释放池。

    基本上,可以这样想:

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