|
From: Thomas H. <t.h...@ph...> - 2011-09-21 16:42:53
|
Dear all, I tried using the valgrind "hello world" example from the quick start page (copied at the bottom of the email), which I compiled using "gcc test.c -O0 -g -o testout". I then ran valgrind with "valgrind --leak-check=yes ./testout". This gave me a memory leak, an invalid write error and an "address ... is 0 bytes after a block of size ..." error as expected. However, the address error and the memory leak claimed the errors were happening in main(), not in f() (full valgrind report is copied below). Thus, it seems to be showing the errors occurring in the wrong function. If I move all the code from f() into main() then the address error and the memory leak are now listed as occurring "(below main)". Is this correct behaviour? Tom The code (taken from http://valgrind.org/docs/manual/quick-start.html)<http://valgrind.org/docs/manual/quick-start.html%29,>: #include <stdlib.h> void f(void) { int* x = malloc(10 * sizeof(int)); x[10] = 0; // problem 1: heap block overrun } // problem 2: memory leak -- x not freed int main(void) { f(); return 0; } Valgrind report: ==8127== Memcheck, a memory error detector ==8127== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==8127== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info ==8127== Command: ./testout ==8127== ==8127== Invalid write of size 4 ==8127== at 0x804843F: f (test.c:6) ==8127== by 0x8048451: main (test.c:11) ==8127== Address 0x41b7054 is 4 bytes after a block of size 40 alloc'd ==8127== at 0x402911D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==8127== by 0x8048451: main (test.c:11) ==8127== ==8127== ==8127== HEAP SUMMARY: ==8127== in use at exit: 40 bytes in 1 blocks ==8127== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==8127== ==8127== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==8127== at 0x402911D: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==8127== by 0x8048451: main (test.c:11) ==8127== ==8127== LEAK SUMMARY: ==8127== definitely lost: 40 bytes in 1 blocks ==8127== indirectly lost: 0 bytes in 0 blocks ==8127== possibly lost: 0 bytes in 0 blocks ==8127== still reachable: 0 bytes in 0 blocks ==8127== suppressed: 0 bytes in 0 blocks ==8127== ==8127== For counts of detected and suppressed errors, rerun with: -v ==8127== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 3 from 3) |