I know a similar question has been answered before here, but I just want to make sure I understand it a little bit better. Here is my scenario…
I have a helper class method that is returning an allocated UIImageView, as seen below.
+(UIImageView *)tableCellButton{
return [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]] autorelease];
}
Then, in one of my UIViewController methods I am using it as such..
UIImageView *imageView = [Helper tableCellButton];
imageView.frame = cell.backgroundView.bounds;
imageView.tag = 250;
[cell.backgroundView addSubview:imageView];
My question is in regards to how this memory is released. I am not using an autorelease pool(other than the application created one), and the variable is not an iVar/Property(so it won’t get released when dealloc is called). In this case am I responsible for releasing the memory after I have called it? When does autorelease come into play? Thanks for the help!
A call to
autoreleasewill causereleaseto be sent to the object the next time through the event loop. That will account for thealloccall you make intableCellButton. The only other time the object is retained is inside ofaddSubview, which will also handle its ownreleaseof the same object. Based on the code above, then, you memory management of this object checks out.