【问题标题】:mutableCopy memory leakmutableCopy 内存泄漏
【发布时间】:2011-04-19 00:05:20
【问题描述】:

谁能解释一下为什么使用 mutableCopy 会导致内存泄漏?

- (id)objectInListAtIndex:(unsigned)theIndex {
       NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"noteNumber"  ascending:YES] autorelease];
       [list sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
       NSMutableArray *theArray = [list mutableCopy];
       NSDictionary *theDict = [theArray objectAtIndex:theIndex];
       return theDict;
 }

【问题讨论】:

    标签: objective-c memory-leaks nsmutablearray


    【解决方案1】:

    因为mutableCopy 返回一个保留对象,而你永远不会释放theArray

    复制方法总是返回一个保留对象,由调用者负责释放。这在API docsmemory management guide 中也有详细说明。

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2013-01-20
      • 2011-10-31
      • 2019-08-10
      相关资源
      最近更新 更多