转储valgrind数据

时间:2012-06-01 07:38:46

标签: valgrind memcheck

我在一个运行无限循环的程序上使用valgrind。

由于memcheck在程序结束后显示内存泄漏,但由于我的程序有无限循环,它将永远不会结束。

那么有什么方法可以强制性地从valgrind中转储数据。

由于

3 个答案:

答案 0 :(得分:8)

查看memcheck的client requests功能。你可以使用VALGRIND_DO_LEAK_CHECK或类似的东西。

编辑:

在回应上述声明时,这不起作用。这是一个永远循环的示例程序:

#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;
}

当我在valgrind中运行时,我会得到无穷无尽的新泄漏:

$ 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

该程序不会终止。

答案 1 :(得分:3)

使用valgrind 3.7.0,你可以从shell中触发(a.o.)泄漏搜索, 使用vgdb。

参见例如http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.monitor-commands (您可以使用vgdb从gdb或shell命令行执行这些监视命令。)

答案 2 :(得分:1)

使用VALGRIND_DO_LEAK_CHECK(acm answer)对我有用 备注:
- 程序必须与valgrind一起启动(valgrind myProg ...)
- 必须安装(有)valgrind-devel包