“不正确的减量”和“潜在泄漏”来自分析器的消息

发布于 2024-12-11 23:20:02 字数 922 浏览 0 评论 0原文

当我使用分析器进行编译时,我收到几条消息。我声明了这些属性:

@property (nonatomic, retain) SyncServicePrimary *syncAndCartOne;
@property (nonatomic, retain) SyncServiceSecondary *syncAndCartTwo;

applicationDidBecomeActive 调用此方法,我得到“分配的对象的潜在泄漏”。

-(void)makeTheCartObjectsForCountry:(NSString*)country_key{
    self.syncAndCartOne = [[SyncServicePrimary alloc] init];
    self.syncAndCartTwo = [[SyncServiceSecondary alloc] init];
}

这在 applicationWillResignActive 中调用;这里我得到“对象引用计数的不正确递减”。

-(void) removeTheCartObjects{
    [self.syncAndCartOne release];
    self.syncAndCartOne = Nil;    
    [self.syncAndCartTwo release];
    self.syncAndCartTwo = Nil; 
}

如果我将对象设置为 autorelease,错误就会消失,但我希望在应用程序隐藏自身时释放对象。

这是我做对的事情,但是分得太远,分析器无法看到开始和结束,或者这是我可以做得更好/正确的事情,这样它就不会抱怨?

我很可能缺少一个关于释放和分配循环的简单概念(我来自 PHP 和 C#)。

When I compile with the analyzer, I get a couple of messages. I have these properties declared:

@property (nonatomic, retain) SyncServicePrimary *syncAndCartOne;
@property (nonatomic, retain) SyncServiceSecondary *syncAndCartTwo;

This method is called from applicationDidBecomeActive and I get "Potential leak of an object allocated".

-(void)makeTheCartObjectsForCountry:(NSString*)country_key{
    self.syncAndCartOne = [[SyncServicePrimary alloc] init];
    self.syncAndCartTwo = [[SyncServiceSecondary alloc] init];
}

This is called in applicationWillResignActive; here I get "Incorrect decrement of the reference count of an object".

-(void) removeTheCartObjects{
    [self.syncAndCartOne release];
    self.syncAndCartOne = Nil;    
    [self.syncAndCartTwo release];
    self.syncAndCartTwo = Nil; 
}

If I set the objects to autorelease, the error goes away, but I want the objects to be released when the app hides itself.

Is this something I am doing right but that is split too far for the analyzer to see the start and end, or is this something I can do better/properly so it won't complain?

Its more than likely that I am missing a simple concept with regard to release and alloc cycles (I've come from PHP and C#).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

枕头说它不想醒 2024-12-18 23:20:02

您的问题在于:

-(void)makeTheCartObjectsForCountry:(NSString*)country_key{
    self.syncAndCartOne = [[SyncServicePrimary alloc] init];
    self.syncAndCartTwo = [[SyncServiceSecondary alloc] init];
}

您正在创建对象,然后保留它们(由于属性声明),因此当只有一个对象引用它们时,它们的引用计数为 2。

你应该这样做:

-(void)makeTheCartObjectsForCountry:(NSString*)country_key{
    SyncServicePrimary *primary = [[SyncServicePrimary alloc] init];
    self.syncAndCartOne = primary;
    [primary release];

    SyncServiceSecondary *secondary = [[SyncServiceSecondary alloc] init];
    self.syncAndCartTwo = secondary;
    [secondary release];
}

Your problem is here:

-(void)makeTheCartObjectsForCountry:(NSString*)country_key{
    self.syncAndCartOne = [[SyncServicePrimary alloc] init];
    self.syncAndCartTwo = [[SyncServiceSecondary alloc] init];
}

You're creating the objects and then retaining them (because of the property declaration), so they have a reference count of 2, when only one object is referencing them.

You should do it like this:

-(void)makeTheCartObjectsForCountry:(NSString*)country_key{
    SyncServicePrimary *primary = [[SyncServicePrimary alloc] init];
    self.syncAndCartOne = primary;
    [primary release];

    SyncServiceSecondary *secondary = [[SyncServiceSecondary alloc] init];
    self.syncAndCartTwo = secondary;
    [secondary release];
}
笑咖 2024-12-18 23:20:02

您已经使用属性 retain 定义了属性,因此分析器假定属性的 setter 方法如下所示:

- (void)setSyncAndCartOne:(SyncServicePrimary *)newValue
{
    [newValue retain];
    [self->_syncAndCartOne release]; // access the instance variable holding the property value
    self->_syncAndCartOne = newValue;
}

如果您使用 @synthesize,则 setter 方法将如下所示那。

因此,当 makeTheCartObjectsForCountry: 返回时,syncAndCartOne 中的对象的保留计数为 2,但应该只有 1 的保留计数。这就是为什么使用 autorelease< /code> 修复它。

出于同样的原因,您不应该执行[self.syncAndCartOne release]。当您将 nil 分配给属性时,setter 方法将向旧对象发送 release

You have defined the properties with attribute retain, so the analyzer assumes that the setter method for the property looks like this:

- (void)setSyncAndCartOne:(SyncServicePrimary *)newValue
{
    [newValue retain];
    [self->_syncAndCartOne release]; // access the instance variable holding the property value
    self->_syncAndCartOne = newValue;
}

If you use @synthesize, the setter method will look like that.

So, when makeTheCartObjectsForCountry: returns, the object in syncAndCartOne has a retain count of 2, but should only have a retain count of 1. That's why using autorelease fixes it.

You shouldn't be doing [self.syncAndCartOne release] for the same reason. The setter method will send the old object a release when you assign nil to the property.

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