How do I release an UIImageView allocated like this?
I've allocated a UIImageView inside the setBackground method of my tableview cell like this:
[cell setBackgroundView:[[UIImageView alloc]initWithImage:rowBackground]];
When i run Analyse in XCode 4 it highlights this 开发者_开发百科line as a possible memory leak. How do i release this UIImageView as Ive not got a pointer to reference from a release call?
You can either allocate it differently (i.e. store it in an ivar and release that), or call autorelease
on it, like this:
[cell setBackgroundView:[[[UIImageView alloc]initWithImage:rowBackground] autorelease]];
Send it an autorelease message:
[cell setBackgroundView:[[[UIImageView alloc]initWithImage:rowBackground] autorelease]];
With an autorelease message you declare that you don't want to own the object beyond the scope in which you sent the message.
精彩评论