内存泄漏(Valgrind报告)(C ++)

时间:2014-06-03 12:40:29

标签: c++ memory-leaks valgrind

这是我的简单代码:

#include <cstdlib>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iostream> 
#include <fstream>
#include <cstring>  
#include <sstream>
#include <map>
using namespace std;

class GeneralMatrix {
protected:
    int width;
    int height;

public:

    //Stores values of matrix size
    GeneralMatrix(int nr, int nc) {
        height = nr;
        width = nc;
    }
};

class RegularMatrix : public GeneralMatrix {
protected:
    vector<double>data;

public:
    //Constructor 
    RegularMatrix(int nr, int nc, const vector<double>& nums) : GeneralMatrix(nr, nc) {
        data = nums;
    }

};

int main(int argc, char** argv) {
 double mm3[] = {10, 2, 3, 0, 0, 8, 0, 4, 2, 2, 6, 0, 0, 0, 0, 5};
 vector<double>k;

 for (int i = 0; i < 16; i++) {
     k.push_back(mm3[i]);

 }
 GeneralMatrix *d = new RegularMatrix(4, 4, k);

 delete d;
    return 0;
}

正如您所看到的,我正在尝试调用构造函数并将其数字向量存储在类内部向量中。一切正常,但Valgrind不知何故发现内存泄漏。

==4903==     in use at exit: 128 bytes in 1 blocks
==4903==   total heap usage: 7 allocs, 6 frees, 396 bytes allocated
==4903== 
==4903== 128 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4903==    at 0x4029F34: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==4903==    by 0x8049679: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (new_allocator.h:104)
==4903==    by 0x8049347: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/ubuntu/Desktop/a.out)
==4903==    by 0x8048E50: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (stl_vector.h:1138)
==4903==    by 0x8048AD5: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (vector.tcc:188)
==4903==    by 0x80489C0: RegularMatrix::RegularMatrix(int, int, std::vector<double, std::allocator<double> > const&) (mem2.cpp:37)
==4903==    by 0x80488AE: main (mem2.cpp:55)
==4903== 
==4903== LEAK SUMMARY:
==4903==    definitely lost: 128 bytes in 1 blocks
==4903==    indirectly lost: 0 bytes in 0 blocks
==4903==      possibly lost: 0 bytes in 0 blocks
==4903==    still reachable: 0 bytes in 0 blocks
==4903==         suppressed: 0 bytes in 0 blocks

任何人都能解释一下我做错了什么吗?谢谢。

编辑:我发布了错误的代码而没有删除(现已修复),问题在于使用向量。

1 个答案:

答案 0 :(得分:3)

您需要使GeneralMatrix的析构函数虚拟,否则您将无法通过基指针正确删除派生对象。

不要混淆指针&amp;引用和动态分配。您可以很好地使用指针或对静态或自动对象的引用。 &#34;多态性仅适用于指针和引用&#34;不是关于对象的生命周期,而是处理对象的方式。请参阅该示例:http://ideone.com/qmywMk

#include <iostream>
struct Base {
    virtual void sayHi() {
        std::cout << "Hi from Base!\n";
    }

    virtual ~Base() {}
};

struct Derived : Base {
    virtual void sayHi() {
        std::cout << "Hi from Derived!\n";
    }
};

void sayHelloToMyValue(Base o) {
    o.sayHi();
}

void sayHelloToMyReference(Base &o) {
    o.sayHi();
}

void sayHelloToMyPointer(Base *o) {
    o->sayHi();
}

int main(int, char**) {
    Derived d;  // No dynamic allocation!
    Base &base = d;

    sayHelloToMyValue(base);
    sayHelloToMyReference(base);
    sayHelloToMyPointer(&base);

    return 0;
}

第一个调用按值(复制)传递base,因此丢失其派生类型。其他两个引用可以路由虚函数调用的原始Derived实例。