I am trying to sort an array of countries. This way works, but I can’t figure out the way to release tmpArray. How do I release it and is there a better way of doing this?
// PUT COUNTRIES IN ARRAY
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"Countries" ofType:@"plist"];
NSArray *tmpArray = [[NSArray alloc] initWithContentsOfFile:myFile];
tmpArray = [tmpArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
arrayCountries = [[NSArray alloc] initWithArray:tmpArray] ;
// [tmpArray release];
Either
-autoreleasethe one you alloc/init’d (because you’re losing your reference to it when you replace it with the sorted array) or use another variable like ‘sortedTmpArray‘.What you’re currently doing is “create this object and assign it to
tmpArray“, then “create another array by filtering this one and assign it totmpArray“. At that point, you no longer have a pointer to the first array you created so there’s no way to release it – it’s leaked.The solution is to place it in the autorelease pool when you create it or just use two separate pointers. Alternatively, you can create a mutable array the first time and use
-sortUsingDescriptors:to sort it in place instead of creating two separate arrays.