【发布时间】:2011-08-02 20:38:37
【问题描述】:
使用 XCode 4 中的分析器,由于设置了如下属性,我收到了潜在内存泄漏的警告:
self.newDog.dogName = self.dogNameTextField.text;
具体的警告是:
属性返回一个具有 +1 保留计数(拥有引用)的 Objective-C 对象。
在第 513 行分配的对象稍后不会在此执行路径中引用,并且保留计数为 +1(对象泄漏)
如果我不使用 self 设置属性,警告就会消失......但我不确定这是否会导致其他问题,因为我读过的所有内容基本上都说在设置/时始终使用 self获取属性:
newDog.dogName = self.dogNameTextField.text;
我在这里做错了什么吗?以下是出现警告的视图控制器中的一些精简代码:
@interface AddDogViewController : UITableViewController {
Dog *newDog;
}
@property (nonatomic, retain) Dog *newDog;
@implementation AddDogViewController
@synthesize newDog;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// some other code
self.newDog.dogName = self.dogNameTextField.text;
// some other code
}
- (void)dealloc {
[newDog release];
[super dealloc];
}
@end
【问题讨论】:
标签: objective-c ios memory-leaks