Jim*_*des 2 memory-leaks objective-c ios
在XCode 4中使用分析器,由于设置了这样的属性,我收到了潜在内存泄漏的警告:
self.newDog.dogName = self.dogNameTextField.text;
Run Code Online (Sandbox Code Playgroud)
具体警告是:
属性返回一个具有+1保留计数(拥有引用)的Objective-C对象.
在第513行分配的对象稍后在此执行路径中未引用,并且保留计数为+1(对象泄漏)
如果我没有使用self设置属性,则警告消失......但我不确定这是否会导致其他问题,因为我读过的所有内容基本上都说在设置/获取属性时始终使用self:
newDog.dogName = self.dogNameTextField.text;
Run Code Online (Sandbox Code Playgroud)
我在这里做错了吗?以下是视图控制器中发生警告的一些精简代码:
@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
Run Code Online (Sandbox Code Playgroud)
根据内存管理规则,一个以new返回+1保留计数开始的方法(就像alloc和copy).您的访问者newDog有这样的名字.
您应该将属性重命名为不能启动的属性new.请注意,你实际上并没有泄漏任何东西.你只是混淆了分析器,因为你违反了命名规则.
| 归档时间: |
|
| 查看次数: |
1161 次 |
| 最近记录: |