开发者

Clarification on IPhone Memory management(autorelease)

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 [[[UIImageVie开发者_开发知识库w 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 autorelease will cause release to be sent to the object the next time through the event loop. That will account for the alloc call you make in tableCellButton. The only other time the object is retained is inside of addSubview, which will also handle its own release of the same object. Based on the code above, then, you memory management of this object checks out.


Each iteration of the runloop has its own autorelease pool.

Essentially, think of it like this:

while(1)
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [someObject doSomething];
    [pool drain];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜