iOS:使用 self 导致内存泄漏?

发布于 2024-11-28 02:20:45 字数 956 浏览 0 评论 0原文

使用 XCode 4 中的分析器,由于设置如下属性,我收到潜在内存泄漏的警告:

self.newDog.dogName = self.dogNameTextField.text;

具体警告是:

  1. Property 返回一个具有 +1 保留计数(拥有引用)的 Objective-C 对象。

  2. 第 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:

  1. Property returns an Objective-C object with a +1 retain count (owning reference).

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-12-05 02:20:45

根据内存管理规则,以new开头的方法返回+1保留计数(就像alloccopy)。您的访问器 newDog 有这样的名称。

您应该将您的属性重命名为不以 new 开头的名称。请注意,您实际上并没有在这里泄漏任何内容。您只是让分析器感到困惑,因为您违反了命名规则。

According to the memory management rules, a method that begins with new returns a +1 retain count (just like alloc and copy). Your accessor newDog 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.

只为守护你 2024-12-05 02:20:45

这是因为当您通过 self.myIvar 访问 ivar 时,您正在使用setter 方法synthesized为此,如果它被声明为 retain,它将向其发送额外的 retain 消息。

如果您直接访问私有变量,即不使用 setter(即不使用 self.myIvar),则不会发生这种情况。

This is because when you access your ivar doing self.myIvar, you are using the setter method that was synthesized to do so, and therefore if it was declared as retain, it will send it an additionaly retain msg.

This will not happen if you access the private variable directly, i.e. without using the setter which is without using self.myIvar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文