退出时释放 LLVM 分配的所有内存
Free all memory allocated by LLVM on exit
我正在使用 LLVM-C 编写一种小玩具语言。
我也在使用 valgrind 来检查内存泄漏。
这是我的基本宝贝程序:
#include <stdio.h>
#include <llvm-c/Core.h>
int main()
{
size_t length;
LLVMModuleRef module = LLVMModuleCreateWithName("llvm.hello");
printf("Module name: %s\n", LLVMGetModuleIdentifier(module, &length));
LLVMDisposeModule(module);
LLVMShutDown();
return 0;
}
我可以像预期的那样正常编译和运行程序。然而,当我通过 valgrind 运行 程序时,它告诉我我有一些像这样的 "still reachable" 分配内存。
valgrind --leak-check=full out/hello_llvm
==5807== LEAK SUMMARY:
==5807== definitely lost: 0 bytes in 0 blocks
==5807== indirectly lost: 0 bytes in 0 blocks
==5807== possibly lost: 0 bytes in 0 blocks
==5807== still reachable: 56 bytes in 2 blocks
==5807== suppressed: 0 bytes in 0 blocks
在这个网站上搜索答案时,我发现许多编码员都说 "still reachable" 内存泄漏不是什么大问题。我不想争论这个。我想要的是在终止我的程序之前摆脱所有分配的内存。
有什么办法可以在终止前将分配的内存减少到零吗?
当 valgrind 不给 0 时,这也让我很紧张,在这种情况下我创建了一个抑制文件,如果你想试一试:
创建并编译一个最小测试:
> cat demo.c
#include <stdlib.h>
int main(void)
{
malloc(10); // leak
}
创建抑制文件:
valgrind --leak-check=full --show-reachable=yes --error-limit=no --gen-suppressions=all --log-file=minimal.supp ./demo
编辑生成的minimal.supp
文件,你会看到类似
的内容
==3102== Memcheck, a memory error detector
==3102== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3102== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==3102== Command: ./demo
==3102== Parent PID: 2633
==3102==
==3102==
==3102== HEAP SUMMARY:
==3102== in use at exit: 10 bytes in 1 blocks
==3102== total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==3102==
==3102== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3102== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==3102== by 0x10915A: main (in /home/david/demo)
==3102==
{
<insert_a_suppression_name_here>
Memcheck:Leak
match-leak-kinds: definite
fun:malloc
fun:main
}
==3102== LEAK SUMMARY:
==3102== definitely lost: 10 bytes in 1 blocks
==3102== indirectly lost: 0 bytes in 0 blocks
==3102== possibly lost: 0 bytes in 0 blocks
==3102== still reachable: 0 bytes in 0 blocks
==3102== suppressed: 0 bytes in 0 blocks
==3102==
==3102== For lists of detected and suppressed errors, rerun with: -s
==3102== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
删除所有以 ==
开头的行并保存为:
{
<my stupid external LLVM leak>
Memcheck:Leak
match-leak-kinds: definite
fun:malloc
fun:main
}
现在 运行 带有抑制文件的 valgrind:
valgrind --tool=memcheck --leak-check=full --show-reachable=yes --error-limit=no --suppressions=minimal.supp ./demo
结果是:
==3348== Memcheck, a memory error detector
==3348== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3348== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==3348== Command: ./demo
==3348==
==3348==
==3348== HEAP SUMMARY:
==3348== in use at exit: 10 bytes in 1 blocks
==3348== total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==3348==
==3348== LEAK SUMMARY:
==3348== definitely lost: 0 bytes in 0 blocks
==3348== indirectly lost: 0 bytes in 0 blocks
==3348== possibly lost: 0 bytes in 0 blocks
==3348== still reachable: 0 bytes in 0 blocks
==3348== suppressed: 10 bytes in 1 blocks
==3348==
==3348== For lists of detected and suppressed errors, rerun with: -s
==3348== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
如您所见,漏洞已从 "definitely lost" 移至 "suppressed"
我正在使用 LLVM-C 编写一种小玩具语言。 我也在使用 valgrind 来检查内存泄漏。
这是我的基本宝贝程序:
#include <stdio.h>
#include <llvm-c/Core.h>
int main()
{
size_t length;
LLVMModuleRef module = LLVMModuleCreateWithName("llvm.hello");
printf("Module name: %s\n", LLVMGetModuleIdentifier(module, &length));
LLVMDisposeModule(module);
LLVMShutDown();
return 0;
}
我可以像预期的那样正常编译和运行程序。然而,当我通过 valgrind 运行 程序时,它告诉我我有一些像这样的 "still reachable" 分配内存。
valgrind --leak-check=full out/hello_llvm
==5807== LEAK SUMMARY:
==5807== definitely lost: 0 bytes in 0 blocks
==5807== indirectly lost: 0 bytes in 0 blocks
==5807== possibly lost: 0 bytes in 0 blocks
==5807== still reachable: 56 bytes in 2 blocks
==5807== suppressed: 0 bytes in 0 blocks
在这个网站上搜索答案时,我发现许多编码员都说 "still reachable" 内存泄漏不是什么大问题。我不想争论这个。我想要的是在终止我的程序之前摆脱所有分配的内存。
有什么办法可以在终止前将分配的内存减少到零吗?
当 valgrind 不给 0 时,这也让我很紧张,在这种情况下我创建了一个抑制文件,如果你想试一试:
创建并编译一个最小测试:
> cat demo.c
#include <stdlib.h>
int main(void)
{
malloc(10); // leak
}
创建抑制文件:
valgrind --leak-check=full --show-reachable=yes --error-limit=no --gen-suppressions=all --log-file=minimal.supp ./demo
编辑生成的minimal.supp
文件,你会看到类似
==3102== Memcheck, a memory error detector
==3102== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3102== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==3102== Command: ./demo
==3102== Parent PID: 2633
==3102==
==3102==
==3102== HEAP SUMMARY:
==3102== in use at exit: 10 bytes in 1 blocks
==3102== total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==3102==
==3102== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3102== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==3102== by 0x10915A: main (in /home/david/demo)
==3102==
{
<insert_a_suppression_name_here>
Memcheck:Leak
match-leak-kinds: definite
fun:malloc
fun:main
}
==3102== LEAK SUMMARY:
==3102== definitely lost: 10 bytes in 1 blocks
==3102== indirectly lost: 0 bytes in 0 blocks
==3102== possibly lost: 0 bytes in 0 blocks
==3102== still reachable: 0 bytes in 0 blocks
==3102== suppressed: 0 bytes in 0 blocks
==3102==
==3102== For lists of detected and suppressed errors, rerun with: -s
==3102== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
删除所有以 ==
开头的行并保存为:
{
<my stupid external LLVM leak>
Memcheck:Leak
match-leak-kinds: definite
fun:malloc
fun:main
}
现在 运行 带有抑制文件的 valgrind:
valgrind --tool=memcheck --leak-check=full --show-reachable=yes --error-limit=no --suppressions=minimal.supp ./demo
结果是:
==3348== Memcheck, a memory error detector
==3348== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3348== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==3348== Command: ./demo
==3348==
==3348==
==3348== HEAP SUMMARY:
==3348== in use at exit: 10 bytes in 1 blocks
==3348== total heap usage: 1 allocs, 0 frees, 10 bytes allocated
==3348==
==3348== LEAK SUMMARY:
==3348== definitely lost: 0 bytes in 0 blocks
==3348== indirectly lost: 0 bytes in 0 blocks
==3348== possibly lost: 0 bytes in 0 blocks
==3348== still reachable: 0 bytes in 0 blocks
==3348== suppressed: 10 bytes in 1 blocks
==3348==
==3348== For lists of detected and suppressed errors, rerun with: -s
==3348== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
如您所见,漏洞已从 "definitely lost" 移至 "suppressed"