如何使扫描构建遵循 __attribute__((cleanup))?

问题描述

我有一个(可笑的)简单程序,它使用 Clang(或 GCC)的 __attribute__((cleanup)) 机制自动释放内存:

#include <stdio.h>
#include <stdlib.h>

void free_memory(void * mem) {
    void ** actual = (void**) mem;
    printf("freeing %p\n",*actual);
    free(*actual);
}

#define autofree __attribute__((cleanup(free_memory)))

int main(int argc,char ** argv) {
    autofree char * str = calloc(51,sizeof(*str));
    printf("str lives at %p\n",str);
    return 0;
}

编译时(仅使用 clang file.c),free_memory 似乎按预期调用

$ ./a.out
str lives at 0x1e2b2a0
freeing 0x1e2b2a0

然而,scan-build 仍然报告潜在的泄漏:

$ export CC=clang
$ scan-build $CC test.c
file.c:15:5: warning: Potential leak of memory pointed to by 'str' [unix.Malloc]
    return 0;
    ^~~~~~~~

我能做些什么来完全抑制这个误报,或者让 scan-build 跟随 __attribute__((cleanup))free 调用? >

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...