Martin Harvey
Martin Harvey

Reputation: 233

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?

Upvotes: 0

Views: 547

Answers (2)

edc1591
edc1591

Reputation: 10182

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]];

Upvotes: 1

albertamg
albertamg

Reputation: 28572

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.

Upvotes: 1

Related Questions