As I’m pretty new to Objective-C and memory management, I was curious if this code was ok –
First of all, code to return a UIImageView –
-(UIImageView *)somethingAnimation {
UIImageView *something = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Something.png"]];
something.frame = CGRectMake(-100, -100, kSomethingWidth, kSomethingHeight);
return something; // released later by the Swarmer object
}
Then the code which calls it, where UIImageView *something is declared in the interface then this is in the implementation –
something = [controller somethingAnimation];
and later –
[something release];
Is this correctly releasing everything? Seems to work without memory or crashing issues. Many thanks for any help.
The Memory Management Programming Guide defines a set of naming conventions that determines whether a method should return an “owned” object or an autoreleased one. According to these conventions, a method named
-somethingAnimationshould definitely return an autoreleased object. So in your case, you should be returning[something autorelease]instead, and then not releasing it later.