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

Memory leaks (Valgrind report)(C++)

本文关键字:c++ 报告 Valgrind 泄漏 内存      更新时间:2023-10-16

这是我的简单代码:

#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
谁能告诉我我做错了什么?谢谢你。

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

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

不要混淆指针&引用和动态分配。您可以很好地使用指向静态或自动对象的指针或引用。"多态性只适用于指针和引用"与对象的生命周期无关,而是处理它的方式。参见示例: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 (copy),因此失去了它的派生类型。