XCode is reporting a memory leak on a specific line of code:
(NSArray*)myFunction{
NSMutableArray * tempMapListings=[[NSMutableArray alloc] init]; //Xcode says leak is here
//do a bunch of stuff to insert objects into this mutable array
return tempMapListings;
[tempMapListings release]; // but I release it ?!
}
Is this due to releasing as an NSArray an mutable array? Since mutable inherits from inmutable, I wouldn’t think this is a problem, and in any case, the object is released anyway. I’d appreciate the advice of a second eye.
You’re releasing
tempMapListingsafter your return from the function. After a return statement, no more code is executed on that branch. Ergo, your[tempListListings release]statement is never run. Moreover, as you’re returning it, you don’t actually want to release it straight away – the caller will never have a chance to retain the array!Autorelease pools are your friend here. Objects added to an autorelease pool are released on your behalf “eventually”, giving your caller time to grab the result. To add your object to the default pool, change your allocation line to
and remove that last
releasecall.For more information on autorelease pools, have a read of Apple’s documentation. They’re really quite useful.