如何让 valgrind 报告 "where" 发生 "definitely lost"
How to let valgrind to report "where" a "definitely lost" happens
有没有办法让 valgrind 报告 "where" 发生了 "definitely lost"?
我要的不是"where it's allocated",而是"where that poor piece of memory got leaked"。
例如这段代码在f()
returns时有"definitely lost"泄漏:
#include <stdlib.h>
void f () {
void *ptr = malloc(42);
}
int main () {
f();
return 0;
}
但 valgrind 只报告分配的来源:
==9772== HEAP SUMMARY:
==9772== in use at exit: 42 bytes in 1 blocks
==9772== total heap usage: 1 allocs, 0 frees, 42 bytes allocated
==9772==
==9772== 42 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9772== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9772== by 0x40053E: f (test.c:4)
==9772== by 0x400552: main (test.c:8)
==9772==
==9772== LEAK SUMMARY:
==9772== definitely lost: 42 bytes in 1 blocks
==9772== indirectly lost: 0 bytes in 0 blocks
==9772== possibly lost: 0 bytes in 0 blocks
==9772== still reachable: 0 bytes in 0 blocks
==9772== suppressed: 0 bytes in 0 blocks
==9772==
==9772== For counts of detected and suppressed errors, rerun with: -v
==9772== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
我正在使用 --leak-check=full
和 --track-origins=yes
。
What I want is not "where it's allocated", but "where that poor piece
of memory got leaked".
不,Valgrind 只报告分配内存的位置,但不报告泄漏的时间和原因。此行为记录在 Memcheck manual:
If --leak-check=full is specified, Memcheck will give details for each
definitely lost or possibly lost block, including where it was
allocated. (Actually, it merges results for all blocks that have the
same leak kind and sufficiently similar stack traces into a single
"loss record". The --leak-resolution lets you control the meaning of
"sufficiently similar".) It cannot tell you when or how or why the
pointer to a leaked block was lost; you have to work that out for
yourself.
另外--track-origins
与内存泄漏无关。它用于跟踪未初始化值的来源。
有没有办法让 valgrind 报告 "where" 发生了 "definitely lost"?
我要的不是"where it's allocated",而是"where that poor piece of memory got leaked"。
例如这段代码在f()
returns时有"definitely lost"泄漏:
#include <stdlib.h>
void f () {
void *ptr = malloc(42);
}
int main () {
f();
return 0;
}
但 valgrind 只报告分配的来源:
==9772== HEAP SUMMARY:
==9772== in use at exit: 42 bytes in 1 blocks
==9772== total heap usage: 1 allocs, 0 frees, 42 bytes allocated
==9772==
==9772== 42 bytes in 1 blocks are definitely lost in loss record 1 of 1
==9772== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9772== by 0x40053E: f (test.c:4)
==9772== by 0x400552: main (test.c:8)
==9772==
==9772== LEAK SUMMARY:
==9772== definitely lost: 42 bytes in 1 blocks
==9772== indirectly lost: 0 bytes in 0 blocks
==9772== possibly lost: 0 bytes in 0 blocks
==9772== still reachable: 0 bytes in 0 blocks
==9772== suppressed: 0 bytes in 0 blocks
==9772==
==9772== For counts of detected and suppressed errors, rerun with: -v
==9772== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
我正在使用 --leak-check=full
和 --track-origins=yes
。
What I want is not "where it's allocated", but "where that poor piece of memory got leaked".
不,Valgrind 只报告分配内存的位置,但不报告泄漏的时间和原因。此行为记录在 Memcheck manual:
If --leak-check=full is specified, Memcheck will give details for each definitely lost or possibly lost block, including where it was allocated. (Actually, it merges results for all blocks that have the same leak kind and sufficiently similar stack traces into a single "loss record". The --leak-resolution lets you control the meaning of "sufficiently similar".) It cannot tell you when or how or why the pointer to a leaked block was lost; you have to work that out for yourself.
另外--track-origins
与内存泄漏无关。它用于跟踪未初始化值的来源。