Do I have to call release on an objective-c retain class variable when setting it to a new object?

Posted by Andrew Arrow on Stack Overflow See other posts from Stack Overflow or by Andrew Arrow
Published on 2010-04-24T22:34:07Z Indexed on 2010/04/24 22:43 UTC
Read the original article Hit count: 191

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa