AlvinfromDiaspar
AlvinfromDiaspar

Reputation: 6814

Release a retain UIImage property loaded via imageNamed?

In my class object i've defined a (nonatomic, retain) property for UIImage. I assigned this property with an image loaded via

[UIImage imageNamed:@"file.png"];

If at some point I want to reassign this property to another image, should I have to release the prior reference?

I am confused because by the retain property I know i should release it. But because imageNamed: is a convenience method (does not use alloc), im not sure what rule to apply here.

Thanks for the insight!

Upvotes: 7

Views: 7237

Answers (5)

sfjava
sfjava

Reputation: 216

Correct, Florin... but per the discussion above, if one is using a setter for the property that (either via synthesize, or manually) does the "retain", then there's no need for the extra retain.

In other words, the following would be correct (and free of memory-leaks), IMHO, am I right? I think this was the original intent of the question... and I'd also like to be certain. ;-) thx!

@interface MyClass {
    UIImage *myImage;
}
@property (nonatomic, retain) UIImage *myImage;
@end

@implementation MyClass
@synthesize myImage;

- (void) someMethod {

    self.myImage = [UIImage imageNamed:@"foo.png"];
}

- (void) someOtherMethod {

    self.myImage = [UIImage imageNamed:@"bar.png"];
}

- (void) dealloc {

    self.myImage = nil;
    [super dealloc];
}
@end

Upvotes: 6

Florin
Florin

Reputation: 528

You should release all the objects you're retaining, however when you define your image object, I believe your code should look like this:

UIImage *img = [[UIImage imageNamed:@"file.png"] retain];

Upvotes: 0

Nathan de Vries
Nathan de Vries

Reputation: 15511

When you define a property with nonatomic & retain, it creates a setter for you that looks like this:

-(void)setImage:(UIImage*)newImage {
  if (image != newImage) {
    [image release];
    image = [newImage retain];
  }
}

As you can see, it releases the previous value before retaining the new value.

In your particular case, the autoreleased image returned by -[UIImage imageNamed:] will be automatically retained when you assign it to the property, and then automatically released when you assign another image (or nil) to the property.

Upvotes: 1

zaph
zaph

Reputation: 112857

The image is returned to you autoreleased as per the naming rules. Assigning it to a property with a retain attribute via the setter will retain it. Assigning another image to the property via the setter will release the old image and retain the new one.

Upvotes: 2

fbrereto
fbrereto

Reputation: 35925

From the docs:

...if you plan to hold onto a returned image object, you must retain it like you would any Cocoa object.

The implication being that if you no longer want to hold onto it, you should release it (assuming you've retained it).

Upvotes: 0

Related Questions