【发布时间】:2012-05-11 16:11:21
【问题描述】:
可能重复:
Local variable assign versus direct assign; properties and memory
Which way is the correct way to allocate memory with a property?
我想知道这两个代码示例之间的区别。
1:
NSString *someString = @"Blabla";
{...Some code...}
imageView.title = [[NSString alloc] initWithString:someString];
2:
NSString *someString = @"Blabla";
{...Some code...}
NSString *str = [[NSString alloc] initWithString:someString];
imageView.title = str;
[str release];
由于某种原因,Xcode Analyzer 警告我选项 #1 可能会导致内存泄漏 - 所以当我将代码更改为选项 #2 时,分析器不会警告我。
有人知道这是什么原因吗?
非常感谢!
【问题讨论】:
-
在选项 2 中,您已经发布了,而在 1 中您还没有。所以警告。
标签: objective-c memory-management