开发者

Telling Clang Static Analyzer about third-party libraries owning references

开发者 https://www.devze.com 2023-02-06 22:59 出处:网络
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 o

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 prefe开发者_StackOverflow中文版r 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.


You can use the new NS_CONSUMED attribute on JS_SetPrivate:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消