开发者

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

开发者 https://www.devze.com 2022-12-28 14:53 出处:网络
Say I have: @pro开发者_JS百科perty (nonatomic,retain) NSString *foo; in some class. And I call: myclass.foo = [NSString stringWithString:@\"string1\"];

Say I have:

@pro开发者_JS百科perty (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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消