mutableCopy内存泄漏

时间:2010-09-29 18:59:19

标签: objective-c memory-leaks nsmutablearray

有人能否解释为什么使用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;
 }

1 个答案:

答案 0 :(得分:7)

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

复制方法始终返回调用者负责释放的保留对象。 API docsmemory management guide中也详细说明了这一点。