为什么valgrind在没有内存时会看到内存泄漏

时间:2018-03-21 19:32:22

标签: c++ memory-leaks polymorphism valgrind

我有以下C ++代码,暂时忽略了在程序中实际执行此操作的不良做法。

#include<iostream>
//#include <type_traits>
using namespace std;

class P{
    public:
    void rebase(P* self);
    virtual void print();
    virtual ~P(){} 
};
class C: public P{
    virtual void print();

};
void P::rebase(P* self){
    //memory leak is detected, but no leak actually happens
    delete self;
    self=new C();
}
void P::print(){
    cout<<"P"<<endl;
}
void C::print(){
    cout<<"C"<<endl;
}


int main(){
    P *test;
    test= new P();
    test->print();
    for(int i=0;i<10000;i++) test->rebase(test);//run the "leaking" code 10000 times to amplify any leak
    test->print();
    delete test;
    while (true);//blocks program from stoping so we can look at it with pmap
}

我通过valgrind发送了这段jenky代码,它报告了P :: rebase()中的内存泄漏,但是当我查看内存使用没有泄漏时,为什么valgrind认为有?

==5547== LEAK SUMMARY:
==5547==    definitely lost: 80,000 bytes in 10,000 blocks
==5547==    indirectly lost: 0 bytes in 0 blocks
==5547==      possibly lost: 0 bytes in 0 blocks
==5547==    still reachable: 72,704 bytes in 1 blocks
==5547==         suppressed: 0 bytes in 0 blocks
==5547== Rerun with --leak-check=full to see details of leaked memory
==5547== 
==5547== For counts of detected and suppressed errors, rerun with: -v
==5547== ERROR SUMMARY: 30001 errors from 7 contexts (suppressed: 0 from 0)

我用sudo pmap -x进行了双重检查,没有泄漏

total kB           13272    2956     180

3 个答案:

答案 0 :(得分:7)

你确实有内存泄漏。

的问题
void P::rebase(P* self){
    //memory leak is detected, but no leak actually happens
    delete self;
    self=new C();
}

您是按值传递指针吗?这意味着来自main的指针永远不会重新分配新地址,并且当函数结束时,self超出范围时实际上会丢失它。如果你使用

void P::rebase(P*& self){
    //memory leak is detected, but no leak actually happens
    delete self;
    self=new C();
}

如果你通过引用传递指针,那么你就不会有内存泄漏。

你的函数中也有未定义,因为你一直在指针上调用delete并且多次调用指针上的delete,如果它不是null,则是未定义的行为。

基本上你的代码与

相同
int* a = new int;
for (int i = 0; i < 10000; i++)
{
    int* b = a; // we copy the address held by a
    delete b; // uh oh we call delete on that same address again
    b = new int; // put new memory in b, this does nothing to a
} // leak here as b goes out of scope and we no longer have the address it held

答案 1 :(得分:1)

Valgrind是正确的。

void P::rebase(P* self){
    //memory leak is detected, but no leak actually happens
    delete self;
    self=new C();
}

这有两个问题 - 它不会返回指向程序的新指针 它删除了一个正在运行的对象。

删除正在运行的对象可能会导致函数末尾的代码崩溃,因为离开函数需要对象引用。

当调用test-&gt; print()时,代码可能会崩溃。我认为编译器正在为两个对象重用内存。如果你改变它们,例如

 P* old = self;
 self=new C();
 delete old;

然后它就行不通了。

对测试的非虚拟调用将起作用,但会导致未定义的行为。由于真实物体在第一次通话后被摧毁。

答案 2 :(得分:0)

Valgrind是一个软件,它有自己的逻辑,并根据其逻辑创建警告列表,如果它的一些条件满足它被认为是内存泄漏。它不一定是完美的内存泄漏。如果您认为可以忽略此类警告,则可以忽略它。

但在你的情况下,我可以看到明显的内存泄漏。您从类C实例化了一个对象,但您永远不会释放内存。这就是C ++的原因,我们鼓励使用智能指针来避免这种错误。

self=new C();