Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17466

C++ Valgrind memory leak

Valgrind tells me this:

==19305== 16 bytes in 1 blocks are definitely lost in loss record 19 of 179
==19305==    at 0x402842F: operator new(unsigned int)
==19305==    by 0x805273E: Loader::createLevel(int, int, std::string, Player*, int, int, int) 
==19305==    by 0x80551B0: Loader::loadLevel()
==19305==    by 0x80676C2: main (main.cpp:38)

My function Loader:.createLevel has got several new statements. How can I know which one of them is causing the leak (i.e., the line)?

Thanks!

P.S.: I'd gladly post the code but it is too long :/

Upvotes: 1

Views: 944

Answers (1)

Gant
Gant

Reputation: 29889

Pass -g option to gcc or g++ so that your executable have debug symbol in them. Here is example from running valgrind on binary with -g.

==20538== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==20538==    at 0x4A05809: malloc (vg_replace_malloc.c:149)
==20538==    by 0x4004F7: main (test.c:8)
==20538==
==20538== LEAK SUMMARY:
==20538==    definitely lost: 4 bytes in 1 blocks.
==20538==      possibly lost: 0 bytes in 0 blocks.
==20538==    still reachable: 0 bytes in 0 blocks.
==20538==         suppressed: 0 bytes in 0 blocks.
==20538== Reachable blocks (those to which a pointer was found) are not shown.
==20538== To see them, rerun with: --show-reachable=yes

gcc -g test.c

This way you can see the line at which the allocation was made.

Upvotes: 3

Related Questions