thierryb
thierryb

Reputation: 3728

Releasing a property (Objective-C)

I have a @property which is defined like this :

@property (nonatomic, retain) name;

In a function I parse some xml and set the name property. My question is should I explicitly release previous retained instance before retain a new one ?

For exemple :

myObj.name = [otherObj getName]; // retain count +1

..

myObj.name = [otherObj getName]; // which is a new instance of a name, is the previous name released ?

Upvotes: 2

Views: 355

Answers (2)

Jim Dovey
Jim Dovey

Reputation: 11156

In a synthesized property setter, the code does something roughly analogous to this (we'll have name be the property name, and _name will be the instance variable to which it refers):

- (void) setName: (NSString *) newValue
{
  if ( newValue == _name )
    return;

  [newValue retain];
  [_name release];
  _name = newValue;
}

Now, that shows what would happen based on your property declaration above. Depending on the other possible attributes of that property, that function would either use [newValue copy] instead of [newValue retain] (if the 'copy' attribute was specified), or it would all be wrapped in an @synchronized(self) block (if the 'nonatomic' attribute was NOT supplied).

I should also note that since your property refers to an NSString, which implements the NSCopying protocol, you should really be using copy rather than retain, i.e.:

@property (nonatomic, copy) NSString * name;

That will cause the synthesized setter to use -copy instead of -retain, which is safer when you're actually passed an NSMutableString object. An immutable string would ultimately only get retained by the -copy call, while a mutable string would create a new immutable copy. Failing to do that means that the string itself could be changed after it's been passed into the setter, changing the value without your object's knowledge.

Upvotes: 4

Jason Coco
Jason Coco

Reputation: 78343

If you have synthesized the accessors for your property (with the @synthesize directive), they will take care of releasing the ivar that is backing the property when the property gets reassigned. The only time you might want to explicitly release the ivar is when your object gets deallocated. So, your dealloc method might look like this:

- (void)dealloc {
  [name release];
  // other cleanup here
  [super dealloc];
}

Upvotes: 3

Related Questions