代码之家  ›  专栏  ›  技术社区  ›  Winston Chen

如何释放即将返回的变量?

  •  1
  • Winston Chen  · 技术社区  · 14 年前

    当我在Xcode中点击build菜单上的“build and analyze”按钮时,我遇到了一个问题。分析建议我释放一个我希望稍后返回的变量。代码如下:

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
    
         //I do some other thing here
    
         MKPinAnnotationView *annView=
            [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"addressLocation"];
    
         //I do some other thing here
    
         return annView;
    }
    

    我可以释放annView并返回它而不引起任何问题吗?

    5 回复  |  直到 14 年前
        1
  •  6
  •   Chuck    14 年前

    这正是自动释放的目的。这种方法应该自动释放它。

    memory management guide 如果你不清楚这类事情。它很短,很好地解释了所有这些东西。一旦你理解了那个指南,你就再也不用怀疑了。

        2
  •  1
  •   Abizern    14 年前

    autorelease ?

        3
  •  1
  •   Matt    14 年前

    有关自动释放池的有用Lynda.com视频,请访问:

    http://creativemac.digitalmedianet.com/articles/viewarticle.jsp?id=1003156
    
        4
  •  1
  •   Alex Nichol    14 年前

    自动释放池是您使用的最佳选择。返回变量时,请执行以下操作:

    return [myVariable autorelease];
    

    苹果的很多方法都是这样的。apple类上的大多数静态构造函数,如[NSString stringWithFormat:]返回自动释放的变量。

        5
  •  -1
  •   adam0101    14 年前