Valgrind 内存泄漏检测

Valgrind memory leak detection

我是 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

我认为这不构成内存泄漏;指针 t 处的内存不是 'lost' 直到 t 超出范围,那是在 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)

代码稍作修改,在 for 循环中多次使用 test* t,实际上 "forgetting" 除最后一个 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)

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

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

我相信每次 t 被分配到另一个内存位置时,如果不释放之前分配的内存,就会构成泄漏(除非你的编译器足够聪明,可以看到分配的内存从未被使用过,并且做了一些聪明的事情——在在这种情况下,通过创建一些使用 t 的示例变量来加倍确定,然后再重新分配 t 甚至 return 其中一个变量...)