rkb
rkb

Reputation: 11231

Is the Object retained in this case

I have a simple question, that in a class I have a variable with property retain

//Classs ArrayClass has this array
@property(nonatomic, retain) NSMutableArray *array;

Now when I do

self.array = [SomeClass getArray];

I need to release the array...

Now If I have object of ArrayClass and when I do

arrayClassObj.array = [SomeClass getArray];

So in this case, Is the setter method is called? Do I need to release in this case.

Upvotes: 0

Views: 131

Answers (2)

Jed Smith
Jed Smith

Reputation: 15944

The setter generated by @synthesize will (since you told it to retain) handle the retaining and releasing for you. If you override it, it's on you.

In your dealloc, don't forget to release it as well -- safe, don't forget, because messages to nil are not errors (and you should be setting a var to nil if you're through with it).

Upvotes: 3

Nick Bedford
Nick Bedford

Reputation: 4435

In both cases the object being assigned it's array property from [SomeClass getArray] will need to release it itself, unless you set the property to nil.

The following is required by the class owning the array property.

// ArrayClassObject dealloc (self in first example)
-(void)dealloc
{
    [array release];
    [super dealloc];
}

When you assign the property then assign it nil, the [array release] is still required in the dealloc method, but since you're assigning nil to it, it won't have any effect.

// someArrayObj must have [array release] in its dealloc method
someArrayObj.array = [SomeClass getArray];

// But you can do it manually by assigning nil
someArrayObj.array = nil;

Upvotes: 2

Related Questions