6

I am using valgrind on a program which runs an infinite loop.

As memcheck displays the memory leaks after the end of the program, but as my program has infinite loop it will never end.

So is there any way i can forcefully dump the data from valgrind time to time.

Thanks

3
  • If you found my answer below solved your problem, it is polite to accept the answer.
    – acm
    Jun 11, 2012 at 15:44
  • thats not what i want. I want to get output on demand and this VALGRIND_DO_LEAK_CHECK does not do that..
    – user414209
    Jun 14, 2012 at 12:45
  • It does for me. I'll update my answer below with an example.
    – acm
    Jun 15, 2012 at 16:27

3 Answers 3

9

Have a look at the client requests feature of memcheck. You can probably use VALGRIND_DO_LEAK_CHECK or similar.

EDIT:

In response to the statement above that this doesn't work. Here is an example program which loops forever:

#include <valgrind/memcheck.h>
#include <unistd.h>
#include <cstdlib>

int main(int argc, char* argv[])
{

  while(true) {
    char* leaked = new char[1];
    VALGRIND_DO_LEAK_CHECK;
    sleep(1);
  }

  return EXIT_SUCCESS;
}

When I run this in valgrind, I get an endless output of new leaks:

$ valgrind ./a.out
==16082== Memcheck, a memory error detector
==16082== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==16082== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==16082== Command: ./a.out
==16082== 
==16082== LEAK SUMMARY:
==16082==    definitely lost: 0 bytes in 0 blocks
==16082==    indirectly lost: 0 bytes in 0 blocks
==16082==      possibly lost: 0 bytes in 0 blocks
==16082==    still reachable: 1 bytes in 1 blocks
==16082==         suppressed: 0 bytes in 0 blocks
==16082== Reachable blocks (those to which a pointer was found) are not shown.
==16082== To see them, rerun with: --leak-check=full --show-reachable=yes
==16082== 
==16082== 1 bytes in 1 blocks are definitely lost in loss record 2 of 2
==16082==    at 0x4C2BF77: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16082==    by 0x4007EE: main (testme.cc:9)
==16082== 
==16082== LEAK SUMMARY:
==16082==    definitely lost: 1 bytes in 1 blocks
==16082==    indirectly lost: 0 bytes in 0 blocks
==16082==      possibly lost: 0 bytes in 0 blocks
==16082==    still reachable: 1 bytes in 1 blocks
==16082==         suppressed: 0 bytes in 0 blocks
==16082== Reachable blocks (those to which a pointer was found) are not shown.
==16082== To see them, rerun with: --leak-check=full --show-reachable=yes
==16082== 
==16082== 2 bytes in 2 blocks are definitely lost in loss record 2 of 2
==16082==    at 0x4C2BF77: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16082==    by 0x4007EE: main (testme.cc:9)
==16082== 
==16082== LEAK SUMMARY:
==16082==    definitely lost: 2 bytes in 2 blocks
==16082==    indirectly lost: 0 bytes in 0 blocks
==16082==      possibly lost: 0 bytes in 0 blocks
==16082==    still reachable: 1 bytes in 1 blocks
==16082==         suppressed: 0 bytes in 0 blocks
==16082== Reachable blocks (those to which a pointer was found) are not shown.
==16082== To see them, rerun with: --leak-check=full --show-reachable=yes

The program does not terminate.

0
4

with valgrind 3.7.0, you can trigger (a.o.) leak search from the shell, using vgdb.

See e.g. http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.monitor-commands (you can do these monitor commands from gdb or from a shell command line, using vgdb).

1

Use of VALGRIND_DO_LEAK_CHECK (acm answer) works for me.
Remarks :
- Program has to be launch with valgrind (valgrind myProg ...)
- valgrind-devel package has to be installed (to have )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.