views:

1218

answers:

5

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!

A: 

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).

fbrereto
A: 

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.

zaph
+1  A: 

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.

Nathan de Vries
A: 

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];
Florin
+3  A: 

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
sfjava
This is correct. using `self.myImage = someImage` calls the setter method, which retains it for you. AND `[UIImage imageNamed:]` returns an autoreleased image, meaning you dont have to clean up after it.
Squeegy