0
votes

Say I have:

@property (nonatomic,retain) NSString *foo;

in some class.

And I call:

myclass.foo = [NSString stringWithString:@"string1"];
myclass.foo = [NSString stringWithString:@"string2"];

Should I have called [myclass.foo release] before setting it to "string2" to avoid a memory leak?

Or the fact that nothing is pointing to the first "string1" object anymore is good enough?

And in the dealloc method [foo release] will be called.

1
You should never ever ever use release on a property accessor like [myclass.foo release].Giao
And please read the Cocoa Memory Management Guide.Paul Lynch

1 Answers

3
votes

From the Apple Docs on declared properties:

retain
Specifies that retain should be invoked on the object upon assignment. (The default is assign.)
The previous value is sent a release message.