I have the following function
- (NSArray *) getUsers : (days) aDay {
NSArray *arr = [[[NSArray alloc] init] autorelease];
for (User *e in [week objectAtIndex:aDay]) {
//Get distance to user
e.place.distance = [[LocationManager sharedLocationManager] getDistanceWithLat:e.place.latitude Lon:e.place.longitude];
}
//Sort the array
arr = [ [week objectAtIndex:aDay] sortedArrayUsingComparator:^(id obj1, id obj2){
User *e1 = (User *)obj1;
User *e2 = (User *)obj2;
if (e1.place.distance <= e2.place.distance) {
return (NSComparisonResult)NSOrderedAscending;
} else {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
[arr retain];
return arr;
}
It leaks and I can’t figure out how to fix it. If I remove [arr retian] then the application crash.
When I preform action profile the instruments display a leak of 100% on this line:
arr = [ [week objectAtIndex:aDay] sortedArrayUsingComparator:^(id obj1, id obj2){
How could I fix this leak?
Remove
NSArray *arr = [[[NSArray alloc] init] autorelease];and[arr retain];at all. In first line you allocated memory, but when sorting you refer to new array and got leak.