Destructor and memory leak

Hi, I have a program to calculate 1+Q+Q^2+...+Q^n, where Q is a polynomial of two variables. It works fine with n=1048, but when I run the program with n=2048, the program stops after 9 minutes with the message "process stopped". I think this could be due to memory leak. Here's my code, I have one class poly of polynomials of one variable, and another class poly2 of polynomials with two variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class poly{
int degre;
bool *coeff;
poly(){coeff=NULL;};
poly(int n, bool *a);
~poly(){delete[] coeff;};
};

class poly2{
int degre2;
poly *coeff2;
poly2(){coeff2=NULL;};
poly2(int n, poly *a);
~poly2(){delete [] coeff2;};

}


After the problem running the program with n=2048, I realized that in the destructor of poly2, I never free the memory allocated to the terms of the array coeff2, which are objects of class poly. So I changed the destructor as follows:
1
2
3
4
5
~poly2(){
int i;
if(coeff2!=NULL){
for(i=0;i<degre2;i++){delete[] coeff2[i].coeff;}}
delete [] coeff2;};


But this times I got error message "*** glibc detected *** ./bun: double free or corruption"

Could anyone tell me what is the problem? Thanks!
Yes line 4 of your change is invalid since coeff2 will delete coeff if not NULL. So remove line 4.

More interesting would be, what you're doing in the constructors poly(int n, bool *a); and poly2(int n, poly *a);
But I need to free the memory allocated to coeff2[i].coeff in the destructor of poly2, no?

Here are the constructors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
poly::poly(int n, bool *a){
degre=n;
coeff=new bool[degre];
int i;
for(i=0;i<degre;i++) coeff[i]=a[i];
}

poly2::poly2(int n, poly *a){
degre2=n;
coeff2=new poly[degre2];
int i;
for(i=0;i<degre2;i++) coeff2[i]=a[i];
}
But I need to free the memory allocated to coeff2[i].coeff in the destructor of poly2, no?
No, like I said poly does it already.


for(i=0;i<degre2;i++) coeff2[i]=a[i];
The assignment above is a problem. You're assigning (indirectly) the pointer to coeff.
That could lead to both: double delete and memory leak
If I use the first version of ~poly2(), it deletes coeff2, but does it call the destructor ~poly() as well? How can I tell?


for(i=0;i<degre2;i++) coeff2[i]=a[i];
The assignment above is a problem. You're assigning (indirectly) the pointer to coeff.
That could lead to both: double delete and memory leak


You mean I'm assigning a objet of class poly to coeff2[i]? Why could this lead to double delete and memory leak?
If I use the first version of ~poly2(), it deletes coeff2, but does it call the destructor ~poly() as well?
Yes

How can I tell?
Make a cout or something in the destructor and test it with a reasonable number

You mean I'm assigning a objet of class poly to coeff2[i]?
Yes

Why could this lead to double delete and memory leak?
Well effectively you do (or better: the compiler does it for you):
coeff2[i]=a[i];->
1
2
coeff2[i].degre=a[i].degre;
coeff2[i].coeff=a[i].coeff; // Here the pointer is copied not(!) the content 
Last edited on
closed account (iAk3T05o)
I'm not sure but i saw this
1
2
3
~poly();
and
~poly2();

is all you need. No { //delete [] blah;}. Just that. Try it
Make a cout or something in the destructor and test it with a reasonable number


Thanks! It does call ~poly().

Well effectively you do (or better: the compiler does it for you):
coeff2[i]=a[i];->

1
2



coeff2[i].degre=a[i].degre;
coeff2[i].coeff=a[i].coeff; // Here the pointer is copied not(!) the content


In fact I have overloaded the = operator(otherwise it wouldn't have worked for N=1048), I didn't put all the class declaration in my first post because it's too long... Here's the = operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
poly &poly::operator =(poly &p){
	if(p.coeff==NULL) {
 	coeff=NULL; return *this;}
	degre=p.degre;
	if (coeff!=NULL) delete [] coeff;
	coeff=new bool[degre];
	int i;
	for(i=0;i<degre;i++){coeff[i]=p.coeff[i];}
	return *this;
}

poly2 &poly2::operator=(poly2 &p){
	if(p.coeff2==NULL) {coeff2=NULL;return *this;}
	degre=p.degre;
	if(coeff2!=NULL) delete [] coeff2;
	coeff2=new poly[degre];
	int i;
	for(i=0;i<degre;i++){coeff2[i]=p.coeff2[i];}
	return * this;

}


edit: in my original code I named the degre of poly2 degre, and not degre2 as in my first post...
Last edited on
I'm not sure but i saw this

1
2
3



~poly();
and
~poly2();


is all you need. No { //delete [] blah;}. Just that. Try it


Thanks. But how can I tell if the default destructor frees the memory?
closed account (iAk3T05o)
They did complain about that. This is what i saw:
1
2
3
4
5
6
7
8
9
~poly()
{
cout << "poly destroyed";
}
and
~poly2()
{
cout << "poly2 destroyed";
}

Did it work? I need to know.
Last edited on

They did complain about that. This is what i saw:

1
2
3
4
5
6
7
8
9



~poly()
{
cout << "poly destroyed";
}
and
~poly2()
{
cout << "poly2 destroyed";
}


Did it work?


This way I only know if the destructors are called, but not whether the allocated memory is freed.
is all you need. No { //delete [] blah;}. Just that. Try it
That's not true. The classes allocate memory so it's their job to deallocate it.

http://en.cppreference.com/w/cpp/language/destructor
A default destructor, if defined, calls other destructors (those may deallocate memory, but the default dtor itself doesn't).
Here's the = operator:
Ok, I see, now you have a [possible] memory leak only.

Move line 5 before line 2
and line 15 before line 13

But how can I tell if the default destructor frees the memory?
It doesn't.


Why do you make it that complicated? You just want an 2 dimensional array of bool which could easily be transposed to a 1 dimensional array
You're right. On both points. We used classes to implement polynomials in C++ class so I did this without thinking...
maeriden, thanks for the link!

coder777, I modified the program as you have said. It works for N=2048 now, though it took some twenty minutes... Thanks!
Topic archived. No new replies allowed.