Reputation: 2302
I'm adding code provided by a partner to my iOS project that calls class_createInstance
and then calls autorelease
before returning, like this:
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:
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.
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.
Upvotes: 2
Views: 340
Reputation: 162722
This is a decidedly odd pattern and, thus, the analyzer simply isn't aware of it. Use of class_createInstance()
is extremely rare.
Two possible solutions off the top of my head:
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).
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.
You should also file a bug via http://bugreporter.apple.com as, though odd, the static analyzer shouldn't barf on that.
Upvotes: 1