【发布时间】:2014-07-12 06:37:00
【问题描述】:
我现在要从 ARC 切换到 MRC,因为我有一个旧项目。但是我对 MRC 不熟悉,现在当我分析我的代码时,可能存在对象属性的泄漏。
这里是代码
@property (nonatomic, retain) NSString *string;
@property (nonatomic, retain) AnotherObject *anotherObject;
- (id)init{
...
_string = [[NSSting alloc] init];
...
}
- (void)doThings{
self.anotherObject.text = self.string; // text is also a retain property of anotherObject,and anotherObject is also a property
}
- (void)dealloc{
[_string release];
[_anotherObject release];
...
}
我尝试修复它,所以我将_string 设为自动释放对象
- (id)init{
...
_string = [[[NSSting alloc] init]autorelease];
...
}
并且仍然在dealloc 方法中释放_string,它可以工作,当我再次分析我的代码时,潜在的泄漏消失了。
但我不知道为什么,谁能帮我解释一下?非常感谢。
【问题讨论】:
-
在您的 init 方法中,通常的模式是
_string = [[NSString alloc] init](我假设这只是一个示例,因为通常您不会像那样创建一个空字符串)。然后[_string dealloc]在您的dealloc方法中。就像你拥有它一样。anotherObject是否被释放,anotherObject是否正确释放dealloc中的text属性? -
是的,
text属性也在dealloc@AaronGolden 中发布 -
我忘了说
anotherObject也是self的retain属性 -
也许
anotherObject正在泄漏并带走字符串。如果你在anotherObject的dealloc方法中设置断点,你打到了吗?
标签: ios objective-c memory-leaks