Valgrind内存泄漏检测

时间:2015-01-01 17:32:19

标签: memory-leaks valgrind

我是Valgrind的新手,我想看看valgrind是如何工作的。我写了一个内存泄漏示例程序。但是Valgrind似乎没有检测到内存泄漏。你能告诉我为什么吗?或者以下代码泄漏内存?

#include <iostream>

using namespace std;

class test {
        private:
                int a;
        public:
                test(int c) {
                        a = c;
                }
};

int main() {
        test* t = new test(7);
}

这是valgrind输出

HEAP SUMMARY:
==5449==     in use at exit: 0 bytes in 0 blocks
==5449==   total heap usage: 29 allocs, 29 frees, 3,592 bytes allocated
==5449== 
==5449== All heap blocks were freed -- no leaks are possible

2 个答案:

答案 0 :(得分:0)

我认为这不构成内存泄漏;指针t的内存不会丢失&#39;直到超出范围,并且在main()结束时,所以没有记忆丧失。

dan@rachel ~ $ g++ -o t t.cpp
dan@rachel ~ $ valgrind ./t
==11945== Memcheck, a memory error detector
==11945== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==11945== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==11945== Command: ./t
==11945==
==11945==
==11945== HEAP SUMMARY:
==11945==     in use at exit: 36 bytes in 9 blocks
==11945==   total heap usage: 9 allocs, 0 frees, 36 bytes allocated
==11945==
==11945== LEAK SUMMARY:
==11945==    definitely lost: 36 bytes in 9 blocks
==11945==    indirectly lost: 0 bytes in 0 blocks
==11945==      possibly lost: 0 bytes in 0 blocks
==11945==    still reachable: 0 bytes in 0 blocks
==11945==         suppressed: 0 bytes in 0 blocks
==11945== Rerun with --leak-check=full to see details of leaked memory
==11945==
==11945== For counts of detected and suppressed errors, rerun with: -v
==11945== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

稍微修改了代码,以便在test* t循环中多次使用for,有效地&#34;遗忘&#34;除了最后一个test对象之外的所有对象。

#include <iostream>
using namespace std;
class test {
  private:
    int a;
  public:
    test(int c){
      a = c;
    }
};

int main(){
  test* t;
  for(int i=1; i<10;i++)
    t=new test(i);
}

要获得更好的内存泄漏修补,请尝试使用调试信息进行编译并使用输出中推荐的valgrind选项:

dan@rachel ~ $ g++ -g -o t t.cpp
dan@rachel ~ $ valgvalgrind --leak-check=full ./t
==11981== Memcheck, a memory error detector
==11981== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==11981== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==11981== Command: ./t
==11981==
==11981==
==11981== HEAP SUMMARY:
==11981==     in use at exit: 36 bytes in 9 blocks
==11981==   total heap usage: 9 allocs, 0 frees, 36 bytes allocated
==11981==
==11981== 36 bytes in 9 blocks are definitely lost in loss record 1 of 1
==11981==    at 0x4C2C099: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==11981==    by 0x4006EF: main (t.cpp:15)
==11981==
==11981== LEAK SUMMARY:
==11981==    definitely lost: 36 bytes in 9 blocks
==11981==    indirectly lost: 0 bytes in 0 blocks
==11981==      possibly lost: 0 bytes in 0 blocks
==11981==    still reachable: 0 bytes in 0 blocks
==11981==         suppressed: 0 bytes in 0 blocks
==11981==
==11981== For counts of detected and suppressed errors, rerun with: -v
==11981== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

答案 1 :(得分:0)

尝试将t分配给另一个测试对象 - 即

int main() {
        test* t = new test(7);
        t = new test(7);
        t = new test(8); // and so on...
}

我相信每次将时间t分配给另一个内存位置,而不释放先前分配的内存将构成泄漏(除非你的编译器足够聪明,看不到分配的内存从未被使用并做了一些聪明的事情 - 在这种情况下,在重新分配t之前创建一些使用t的样本变量,并且甚至可以返回其中一个变量,从而加倍确定......)