告诉 Clang Static Analyzer 有关拥有引用的第三方库的信息
我维护一个 Objective-C 项目,该项目使用实现垃圾收集脚本环境的 C 库。在某些情况下,我需要将保留的 Objective-C 对象放入脚本对象的私有字段中。然后 Objective-C 对象在 Finalize 回调中被释放。
设置私有值的调用看起来像这样,希望有明显的语义:
if (!JS_SetPrivate(context, jsSelf, [self retain])) /* handle error */
Finalize 回调执行以下操作:
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);
}
Clang 静态分析器不反对 Finalize 回调中的随机释放,但在最初设置该值的地方,它会显示“潜在泄漏”在第 N 行分配的对象。”
是否有注释或不难看的模式可以抑制此消息? (我不想做像 [object PerformSelector:@selector(retain)]
这样的愚蠢事情。我也不想弄乱声明 JS_SetPrivate
的标头。另请注意,赋予 JS_SetPrivate
的值是任意指针,不一定是 Objective-C 对象。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 JS_SetPrivate 上使用新的 NS_CONSUMED 属性:
http://clang-analyzer.llvm.org /annotations.html#attr_ns_consumed
You can use the new NS_CONSUMED attribute on JS_SetPrivate:
http://clang-analyzer.llvm.org/annotations.html#attr_ns_consumed