iOS:使用 self 导致内存泄漏?
使用 XCode 4 中的分析器,由于设置如下属性,我收到潜在内存泄漏的警告:
self.newDog.dogName = self.dogNameTextField.text;
具体警告是:
Property 返回一个具有 +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
Using the analyzer in XCode 4, I'm getting warnings of potential memory leaks due to setting a property like this:
self.newDog.dogName = self.dogNameTextField.text;
The specific warning is:
Property returns an Objective-C object with a +1 retain count (owning reference).
Object allocated on line 513 is not referenced later in this execution path and has a retain count of +1 (object leaked)
If I don't set the property using self, the warning goes away... but I'm not sure if that won't cause other issues since everything I've read basically says to always use self when setting/getting properties:
newDog.dogName = self.dogNameTextField.text;
Am I doing something else wrong here? Here's some stripped down code from the view controller where the warnings occur:
@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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据内存管理规则,以
new
开头的方法返回+1保留计数(就像alloc
和copy
)。您的访问器newDog
有这样的名称。您应该将您的属性重命名为不以
new
开头的名称。请注意,您实际上并没有在这里泄漏任何内容。您只是让分析器感到困惑,因为您违反了命名规则。According to the memory management rules, a method that begins with
new
returns a +1 retain count (just likealloc
andcopy
). Your accessornewDog
has such a name.You should rename your property to something that does not start with
new
. Note that you're not actually leaking anything here. You're just confusing the analyzer because you're violating the naming rules.这是因为当您通过
self.myIvar
访问ivar
时,您正在使用setter
方法synthesized
为此,如果它被声明为retain
,它将向其发送额外的retain
消息。如果您直接访问私有变量,即不使用 setter(即不使用 self.myIvar),则不会发生这种情况。
This is because when you access your
ivar
doingself.myIvar
, you are using thesetter
method that wassynthesized
to do so, and therefore if it was declared asretain
, it will send it an additionalyretain
msg.This will not happen if you access the private variable directly, i.e. without using the setter which is without using
self.myIvar
.