在class_createInstance返回的对象上调用自动释放时分析错误

时间:2021-11-08 09:36:59

I'm adding code provided by a partner to my iOS project that calls class_createInstance and then calls autorelease before returning, like this:

我正在将我的iOS项目中的合作伙伴提供的代码添加到调用class_createInstance,然后在返回之前调用autorelease,如下所示:

Class functionClass = objc_getClass(functionName);
NSObject* functionObject = class_createInstance(functionClass, 0);
[[functionObject performSelector:@selector(initWithDictionary:) withObject:msg] autorelease];

When running Analyze in Xcode 4.0.2, I get the following warning on the last line:

在Xcode 4.0.2中运行Analyze时,我在最后一行收到以下警告:

Object sent -autorelease too many times

Question 1: How is that object getting sent autorelease too many times?
My understanding of class_createInstance is that I need to release the value it returns.

问题1:该对象如何被多次发送自动释放?我对class_createInstance的理解是我需要释放它返回的值。

Question 2: If the code is correct, how can I avoid the warning from Analyze?
We have a pretty strict policy to not check in any Analyze warnings.

问题2:如果代码正确,我该如何避免分析警告?我们有一个非常严格的政策,不检查任何分析警告。

1 个解决方案

#1


1  

This is a decidedly odd pattern and, thus, the analyzer simply isn't aware of it. Use of class_createInstance() is extremely rare.

这是一个非常奇怪的模式,因此,分析仪根本不知道它。使用class_createInstance()非常罕见。

Two possible solutions off the top of my head:

我头脑中有两种可能的解决方案:

  1. Use the preprocessor markup to tell the analyzer that, yes, in fact, functionObject is a retained reference (I don't have the markup handy -- you'll find it in the release notes or on the llvm.org site or search the system headers).

    使用预处理器标记告诉分析器,是的,事实上,functionObject是一个保留的引用(我没有标记方便 - 你可以在发行说明或llvm.org网站上找到它或搜索系统标题)。

  2. Don't use class_createInstance(). Once you have a reference to the class, just use +alloc. Better yet, rewrite the entire expression as [[objc_getClass(functionName) alloc] initWithDictionary:msg] and be done with it.

    不要使用class_createInstance()。一旦你有了对类的引用,只需使用+ alloc。更好的是,将整个表达式重写为[[objc_getClass(functionName)alloc] initWithDictionary:msg]并完成它。

You should also file a bug via http://bugreporter.apple.com as, though odd, the static analyzer shouldn't barf on that.

您还应该通过http://bugreporter.apple.com提交一个错误,因为虽然很奇怪,静态分析器不应该对此进行限制。

#1


1  

This is a decidedly odd pattern and, thus, the analyzer simply isn't aware of it. Use of class_createInstance() is extremely rare.

这是一个非常奇怪的模式,因此,分析仪根本不知道它。使用class_createInstance()非常罕见。

Two possible solutions off the top of my head:

我头脑中有两种可能的解决方案:

  1. Use the preprocessor markup to tell the analyzer that, yes, in fact, functionObject is a retained reference (I don't have the markup handy -- you'll find it in the release notes or on the llvm.org site or search the system headers).

    使用预处理器标记告诉分析器,是的,事实上,functionObject是一个保留的引用(我没有标记方便 - 你可以在发行说明或llvm.org网站上找到它或搜索系统标题)。

  2. Don't use class_createInstance(). Once you have a reference to the class, just use +alloc. Better yet, rewrite the entire expression as [[objc_getClass(functionName) alloc] initWithDictionary:msg] and be done with it.

    不要使用class_createInstance()。一旦你有了对类的引用,只需使用+ alloc。更好的是,将整个表达式重写为[[objc_getClass(functionName)alloc] initWithDictionary:msg]并完成它。

You should also file a bug via http://bugreporter.apple.com as, though odd, the static analyzer shouldn't barf on that.

您还应该通过http://bugreporter.apple.com提交一个错误,因为虽然很奇怪,静态分析器不应该对此进行限制。