+(NSMutableDictionary*)getList:(NSString *)key
{
    ......
    NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key];
    return dict;
}
调用时
NSMutableDictionary *obj = [self getList:key];
[obj.release];Instruments中偶尔提示内存泄漏 指向
NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key];请教问题出在哪里?

解决方案 »

  1.   

    +(NSMutableDictionary*)getList:(NSString *)key
    {
      //使用autorelease
      [NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key] autorelease];
      return dict;
    }
      

  2.   

    +(NSMutableDictionary*)getList:(NSString *)key
    {
      //使用autorelease
      NSMutableDictionary *dict=[[[NSMutableDictionary alloc] initWithContentsOfFile:key] autorelease];
      return dict;
    }
      

  3.   

    1,NSMutableDictionary *obj = [self getList:key];
    [obj.release];
    是不对的,肯定报错的
    因为(getList:)是+2,代码在释放池中的话,可以返回 autorelease
    或者调用后自动释放
    NSMutableDictionary *obj = [类 getList:key];
    这里如果是在普通函数中的话,大可不用管。因为函数结束后,所有的引用地址会自动释放。 *obj是指针,没有alloc。
      

  4.   

    使用autorelease会报错 obj为空
    我现在需要obj持久保持的 
      

  5.   

    我指的obj为空 是在窗口切换后为空 第一次加载时数据是有的.
      

  6.   

    obj 设置为类中的成员变量,   obj=[[NSMutableDictionary alloc] init];obj=[self getlist:key];试试看。
      

  7.   

    补充一点。。
    在该方法内,记得obj release 一次。dealloc 中,也要release 一次
      

  8.   

    最佳写法:+(NSMutableDictionary*)getList:(NSString *)key
    {
       ......
       NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithContentsOfFile:key];
       return [dict autorelease];
    }记得外部不需要release了。