Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

shawnl

macrumors newbie
Original poster
Sep 16, 2008
4
0
I have a UIImage property which I need to be able to change the UIImage on the fly.

Currently I do this like so...

if (activeImage != nil)
{
[activeImage release];
activeImage = nil;
}

activeImage = image;

The problem I have noticed is as soon as I call the release it shows FREEID(id) in the debugger popup where it used to show UIImage *... so I am thinking that this is not the correct way to release the old resources before filling the new ones... suggestions are appreciated!
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
If that is the entirety of the method why not just synthesize the accessors? That way you can guarantee it's correct.

Otherwise iirc this is the "correct" way:

Code:
- (void) setImage:(UIImage *) theImage
{
UIImage *temp;
if (theImage != image) // This avoids a retain/release cycle if we are trying to set the same image as currently is in use.
{
  temp = image; // I have assumed the property is called image
  image = [theImage retain]; // We can message nil objects so this should be fine
  [temp release];
}
}
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
As Nutter mentioned there, the temp var isn't necessary.

Code:
- (void) setImage:(UIImage *) theImage {
	if (theImage != image) {
		[image release];
		image = [theImage retain];
	}
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.