If I allocate an object in a method, ‘getASprocket’ and call it this way, will there be a leak here?
Sprocket *sprock = [Sprocket getASprocket];
// store this returned value as an ivar
ivarSprock = [sprock retain];
// release the originally acquired object
[sprock release];
The Sprocket object is allocated and returned this way:
– (Sprocket *)getASprocket {
Sprocket *sprocket;
sprocket = [[Sprocket alloc] init];
return [sprocket retain];
}
Also, would changing from '[sprocket retain];' inside the ‘aSprocket’ method to 'return [sprocket autorelease];' make worse performance hit?
Please see this good explanation page Especially sub-page #7
each retain created another memory object. so lets see what we’ve got:
in
getASprocket:+1
+1
and on your method:
+1
-1
What should we do?
Well we should let the Sprock go by making it autorelease:
or in the words of the reffered link above: