0

the code is just simple:

@interface Test : NSObject
@property (nonatomic, strong) NSString * str;  //strong
//@property (nonatomic, weak) NSString * str;  //weak
-(void)func;
@end

@implementation Test
@synthesize str = _str;
-(void)func{
   _str = @"test string";       // using ivar
   //self.str = @"test string"; // using setter
}
@end

there are four situations in the code above, strong/weak, ivar/setter

which types will cause memory leaks ?

which types are the same ?

I have test the code with NSLog but all run well (no nil printed) , why ? maybe about autorelease ?

--------------edit---------------

i read the document and find that "string constant would be never deallocated"

so the code act different when string initializes with initWithString or initWithFormat (and the code i wrote is wrong )

the weak property always be nil when using initWithFormat

for memory management ivar and setter are the same: Is self.iVar necessary for strong properties with ARC?

4

2 回答 2

0

我也总是对此感到困惑,但是我认为基本上 weak 意味着当类设置为 nil 时它会消失,而 strong 不会让类消失,直到 var 也被删除,因为它具有 +1 保留计数开始。

您不能真正问哪个会导致泄漏,因为正确使用也不会导致泄漏,并且两者都不能正确使用。

于 2012-06-10T21:32:48.797 回答
0

如果您使用 ARC,系统会处理内存并且不会报告为泄漏。这是一篇很好的文章,可以检查 ARC 以及强引用和弱引用之间的区别。

http://www.quora.com/Objective-C-programming-language/In-Objective-C-whats-the-difference-between-a-strong-and-weak-pointer

于 2012-06-10T17:56:09.880 回答