Memory leak in class pointer as return value of function

Post questions regarding programming in C/C++ in Linux/Unix.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Memory leak in class pointer as return value of function

Postby liy39 » Tue Jun 26, 2012 11:53 pm

Hello everyone,

Can anyone help me with memory leak. I found memory leak with the pointer Matrix *cc = new Matrix(m) in the function below when I used valgrind check the memory usage. 16 bytes in 1 blocks are definitely lost.


Matrix Matrix::Resize(int m){
Matrix *cc = new Matrix(m);//memory leak here
for (int i=0;i<m;i++){
for (int j=0;j<m;j++){
cc->val[i][j]=val[i][j];
cout<< cc->val[i][j] << endl;
}
}
return *cc;
cerr << "post return\n";
/*delete [] cc->val;
delete & cc;
delete [] cc;*/
}

In the main program (shown below), another pointer was defined and the allocated memory was released by using delete.It seems that 'delete [] cc' doesn't work in the function after return value. Is there any way to release memory assigned within this function? Or is there any other way to make this program work? Thanks in advance.

Here is the whole program:

#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix(){val = NULL;num=0;};
Matrix(int);
Matrix(int, double);
~Matrix(){
for (int i=0;i<num;i++){
delete [] val[i];
}
delete [] val;
};
Matrix Resize(int m);
private:
double **val;
int num;
};

Matrix::Matrix(int n){
num=n;
val = new double * [n];
for (int i=0;i<n;i++){
val[i] = new double [n];
for (int j=0;j<n;j++){
val[i][j]=0;
cout<< val[i][j] << endl;
}
}

};

Matrix::Matrix(int n, double dd){
num=n;
val = new double * [n];
for (int i=0;i<n;i++){
val[i] = new double [n];
for (int j=0;j<n;j++){
val[i][j]=dd;
cout<< val[i][j] << endl;
}
}
};

Matrix Matrix::Resize(int m){
Matrix *cc = new Matrix(m); // cause memory leak: 16 bytes in 1 blocks are definitely lost in loss
for (int i=0;i<m;i++){
for (int j=0;j<m;j++){
cc->val[i][j]=val[i][j];
cout<< cc->val[i][j] << endl;
}
}
return *cc;
cerr << "post return\n";
/*delete [] cc->val;
delete & cc;
delete [] cc;*/
}


main()

{
Matrix *cc1 = new Matrix(4, 5.5); // No memory leak because it can be released using 'delete'.
Matrix c=cc1->Resize(1);
delete cc1;

}
liy39
 
Posts: 1
Joined: Tue Jun 26, 2012 11:44 pm

Re: Memory leak in class pointer as return value of function

Postby exomo » Wed Jun 27, 2012 1:45 pm

Well, return ends the execution of a function, so of course any code after a return statement is not executed. And your function is of no use when you delete the allocated memory within the function.
What exactly are you trying to do? I don't think it is very useful to have a matrix object create and return another matrix object.
The solution to your problem is most likely to avoid pointers. Use objects instead of pointers wherever possible. Implement copy constructor and assignment operator for you class and never allocate objects manually.
Use it like
Code: Select all
Matrix cc1(4, 5.5);
Matrix c = cc1.Resize(1); // if you really want this strange resize function
If you don't use new you don't have to care about the delete[], it's that simple.

PS: The simple answer would be: The Resize method should return the pointer, so the memory address is not lost and you can delete it in the main function. But I really think you should take the long way and fix the structure of your code.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Memory leak in class pointer as return value of function

Postby Wizard » Fri Jul 06, 2012 7:48 am

Specifically, this line
Matrix *cc = new Matrix(m);
creates a new matrix on the heap, and this line
return *cc;
copies that matrix from the heap onto the stack and returns it. The one on the heap is still there. That's your leak. As Exomo says, you can fix this by returning the pointer instead (simply "return cc;" without the *) and handle deleting the pointer in the main program, but I agree with him that your logic needs to be reworked.
User avatar
Wizard
Site Admin
 
Posts: 3226
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA


Return to Unix/Linux

Who is online

Users browsing this forum: No registered users and 2 guests