mutableCopy内存泄漏

0 memory-leaks objective-c 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;
 }
Run Code Online (Sandbox Code Playgroud)

mip*_*adi 7

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

复制方法始终返回一个保留对象,调用者负责释放该对象.这也在API文档内存管理指南中有详细说明.