“不正确的减量”和“潜在泄漏”来自分析器的消息
当我使用分析器进行编译时,我收到几条消息。我声明了这些属性:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题在于:
您正在创建对象,然后保留它们(由于属性声明),因此当只有一个对象引用它们时,它们的引用计数为 2。
你应该这样做:
Your problem is here:
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:
您已经使用属性
retain
定义了属性,因此分析器假定属性的 setter 方法如下所示:如果您使用
@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:If you use
@synthesize
, the setter method will look like that.So, when
makeTheCartObjectsForCountry:
returns, the object insyncAndCartOne
has a retain count of 2, but should only have a retain count of 1. That's why usingautorelease
fixes it.You shouldn't be doing
[self.syncAndCartOne release]
for the same reason. The setter method will send the old object arelease
when you assignnil
to the property.