2010-09-29 54 views
0

任何人都可以闡明爲什麼這種使用mutableCopy會泄漏內存?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; 
} 

回答

7

因爲mutableCopy返回保留的對象,你永遠不放開theArray

複製方法總是返回調用者負責釋放的保留對象。這在API docsmemory management guide中也有詳細說明。