I often see something like:
NSArray *tmpArr = [[NSArray alloc] initWithObjects:@"Info", nil];
self.userInfo = tmpArr;
[tmpArr release];
instead of:
self.userInfo = [[NSArray alloc] initWithObjects:@"Info", nil];
Does anyone know why the top code sample is more popular? Is it more correct memory management than the second?
Second code snippet causes a memory leak due to the array not being released. In most cases properties of object types (like
NSArrayin this case) are eitherretainorcopyproperties and this means they either increase the reference count of the assigned value or copy the whole object. Then the local variable can be (and should be) released if it’s not needed anymore.Non-leaking alternative to the second code snipped would be using
autorelease:or simply: