Memory leaks Valgrind内存泄漏检测

Memory leaks Valgrind内存泄漏检测,memory-leaks,valgrind,Memory Leaks,Valgrind,我是Valgrind的新手,我想看看Valgrind是如何工作的。我为内存泄漏编写了一个示例程序。然而,Valgrind似乎没有检测到内存泄漏。你能告诉我为什么吗?或者下面的代码是否泄漏内存 #include <iostream> using namespace std; class test { private: int a; public: test(int c) {

我是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);
}

我不认为这构成了内存泄漏;指针
t
处的内存在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
,有效地“忘记”了除最后一个
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);
}

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

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

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

Valgrind应该报告此代码的内存泄漏。您是否使用静态链接?请参阅,是的。我使用了错误的语法。Dan,谢谢。我的初始代码现在运行正常。看起来我一直在使用错误的语法运行Valgrind。感谢您的麻烦。很高兴我能提供帮助!同时确保签出
--leak check=full
请记住,如果您使用调试信息编译代码,您将获得泄漏的行号。(我更新了代码,将
-g
参数显示为
g++
和更新的输出)。
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)
int main() {
        test* t = new test(7);
        t = new test(7);
        t = new test(8); // and so on...
}