Valgrind内存泄漏摘要:明确丢失

huangapple go评论88阅读模式
英文:

valgrind LEAK SUMMARY: definitely lost

问题

Memory leak is happening in the Data::getX() method. The memory allocated for the Data object in this method is not being freed before returning. To fix the memory leak, you should delete the Data object before returning it. Here's the corrected version of the Data::getX() method:

Data* Data::getX(){
    if(this->col <= 0) throw missing_data();
    int r = this->row;
    int c = this->col - 1;
    Data *data = new Data(r, c, this->data);

    // Free the memory before returning
    for(int i = 0; i < r; i++){
        delete[] this->data[i];
    }
    delete[] this->data;

    return data;
}

In this corrected version, the memory allocated for this->data is properly freed before creating the new Data object. This should resolve the memory leak issue.

英文:

I write a class 'Data' as following.

//code in .h file
class Data{
private:
    int col = 0;
    int row = 0;
    float **data = NULL;
public:
    //Constructor
    Data();
    Data(int r, int c);
    Data(int r, int c, float **data1);
    Data* getX();
};

//code in .cpp file
Data::~Data(){
    for(int i = 0; i &lt; this-&gt;row; i++){
        delete[] this-&gt;data[i];
        this-&gt;data[i] = nullptr;
    }
    this-&gt;row = 0;
    this-&gt;col = 0;
}

Data::Data(){
    this-&gt;col = 0;
    this-&gt;row = 0; 
    this-&gt;data = nullptr; 
}


Data::Data(int r, int c,float **data1){
    this-&gt;row = r;
    this-&gt;col = c;
    this-&gt;data = new float*[r];
    for (int i = 0; i &lt; r; i++){
        this-&gt;data[i] = new float[c];
        memcpy(this-&gt;data[i],data1[i],sizeof(float)*c);
    }
}
Data* Data::getX(){
    if(this-&gt;col &lt;= 0) throw missing_data();
    int r = this-&gt;row;
    int c = this-&gt;col - 1;
    Data *data = new Data(r,c,this-&gt;data);
    return data;
}

It can compile and run normally. Then when I scan the code with valgrind, it prompts that the code has a memory leak.

==1247551== 400 bytes in 5 blocks are definitely lost in loss record 181 of 182
==1247551==    at 0x483E217: operator new[](unsigned long) (vg_replace_malloc.c:640)
==1247551==    by 0x11C074: Data::Data(int, int, float**) 
==1247551==    by 0x11C707: Data::getX() 

Where is the memory leak problem in this code?
Thank you!
Here is my code in main().

    Data *X = this-&gt;data-&gt;getX();
    GHInfoAF&lt;float&gt; GandH = Divide::APdividebinning(set,ghinfo,X,sbtp-&gt;getMaxBining(),tempqpoint);
    G_H.push_back(GandH); 
    delete X;
    X = nullptr; 

Where is the memory leak problem in this code?

答案1

得分: 1

在析构函数中,我忘记释放指针 this->data。所以,当我添加

delete[] this->data;
this->data = nullptr;

问题已经解决。

英文:

In the destructor, I forgot to release the pointer this->data.
So, when I add

delete[] this-&gt;data; 
this-&gt;data = nullptr;

it has been fixed.

huangapple
  • 本文由 发表于 2023年4月17日 16:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定