问题描述
我有一个(可笑的)简单程序,它使用 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 (将#修改为@)