Question

I maintain an Objective-C project which uses a C library that implements a garbage-collected scripting environment. In several cases, I need to put a retained Objective-C object in the private field of a scripting object. The Objective-C object is then released in a finalize callback.

The call to set the private value looks like this, with hopefully obvious semantics:

if (!JS_SetPrivate(context, jsSelf, [self retain])) /* handle error */

The finalize callback does this:

id object = JS_GetPrivate(context, jsSelf);
if (object != nil)
{
    [object clearJSSelf:jsSelf]; // Remove reference to JS wrapper.
    [object release];  // JS wrapper owned a reference.
    JS_SetPrivate(context, jsSelf, nil);
}

The Clang Static Analyzer has no objection to the random release in the finalize callback, but where the value is initially set it says “Potential leak of an object allocated on line N.”

Is there an annotation or non-ugly pattern that would suppress this message? (I’d rather not be doing silly things like [object performSelector:@selector(retain)]. I’d also prefer not to mess with the header declaring JS_SetPrivate. Also note that the value given to JS_SetPrivate is an arbitrary pointer, not necessarily an Objective-C object.

Was it helpful?

Solution

You can use the new NS_CONSUMED attribute on JS_SetPrivate:

http://clang-analyzer.llvm.org/annotations.html#attr_ns_consumed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top