告诉 Clang Static Analyzer 有关拥有引用的第三方库的信息

发布于 2024-10-14 23:02:18 字数 824 浏览 5 评论 0原文

我维护一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2024-10-21 23:02:18

您可以在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文