【问题标题】:dump valgrind data转储 valgrind 数据
【发布时间】:2012-06-06 11:02:48
【问题描述】:

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

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

那么有什么办法可以不时强制转储 valgrind 中的数据。

谢谢

【问题讨论】:

  • 如果您发现下面我的回答解决了您的问题,请接受回答。
  • 那不是我想要的。我想按需获得输出,而这个 VALGRIND_DO_LEAK_CHECK 不这样做..
  • 对我有用。我将在下面用一个例子更新我的答案。

标签: valgrind memcheck


【解决方案1】:

看看memcheckclient 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

程序不会终止。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      使用 VALGRIND_DO_LEAK_CHECK(acm 答案)对我有用。
      备注:
      - 程序必须使用 valgrind (valgrind myProg ...)
      - 必须安装 valgrind-devel 包(必须)

      【讨论】:

        猜你喜欢
        • 2018-05-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 2018-02-12
        • 2020-08-12
        • 2011-12-07
        相关资源
        最近更新 更多