I have a problem freeing up memory. First I go to show my constructors , destructors and private part of the clases:
Class 1:
class VectorDinamico {
private:
int util, tam;
int *vect;
/*Rest of the class*/
}
VectorDinamico::VectorDinamico() {
util = 0;
tam = 10;
vect = new int[10];
}
VectorDinamico::VectorDinamico(const VectorDinamico &v){
vect = new int[v.tam];
for (int i = 0 ; i < v.util; i++) {
vect[i] = v.vect[i];
}
util = v.util;
tam = v.tam;
}
VectorDinamico::~VectorDinamico() {
delete []vect;
}
VectorDinamico& VectorDinamico::operator= (const VectorDinamico &v) {
if (this != &v) {
delete []vect;
vect = new int[v.tam];
for (int i = 0 ; i < v.util; i++) {
vect[i] = v.vect[i];
}
util = v.util;
tam = v.tam;
}
return *this;
}
Works fine ( Valgrind: nothing in use at exit)
Class 2:
class Conjunto {
private:
VectorDinamico datos;
/*Rest of the class*/
}
//Is empty because it uses the VectorDinamico constructor( I think )
Conjunto::Conjunto() {}
Conjunto::Conjunto( const Conjunto &c) {
datos = c.datos; // = Is overloaded in VectorDinamico
}
//Is empty because it uses the VectorDinamico destructor( I think )
Conjunto::~Conjunto() {};
Conjunto& Conjunto::operator=( const Conjunto &c) {
if (this != &c)
datos = c.datos;
return *this;
}
Works fine ( Valgrind: nothing in use at exit)
Class 3: ( I think here is the problem )
class SetConjuntos{
private:
Conjunto *conjuntos;
int util, tam;
/*Rest of the class*/
}
SetConjuntos::SetConjuntos(){
util = 0;
tam = 10;
conjuntos = new Conjunto[10];
}
SetConjuntos::SetConjuntos(const SetConjuntos &sc){
util = sc.util;
tam = sc.tam;
conjuntos = new Conjunto[tam];
for(int i = 0 ; i < util ; i++)
conjuntos[i]=sc.conjuntos[i];
}
//Here is my problem ( I think )
//anyway I've left it empty, because I think it uses the Conjunto destructor's
SetConjuntos::~SetConjuntos(){
//delete []conjuntos; ( Cause segmetation fault )
}
I think the SetConjuntos destructor is not correct because the Valgrind output is:
==6838== LEAK SUMMARY:
==6838== definitely lost: 2,912 bytes in 4 blocks
==6838== indirectly lost: 11,320 bytes in 180 blocks
How must be the destructor for SetConjuntos ?
Thanks
-----------Solved adding operator= to SetConjuntos----------------
I had not implemented the assignment operator, because he thought that if he did not use explicitly was not necessary. I was wrong.
Now class 3 is:
class SetConjuntos{
private:
Conjunto *conjuntos;
int util, tam;
/*Rest of the class*/
}
SetConjuntos::SetConjuntos(){
util = 0;
tam = 10;
conjuntos = new Conjunto[10];
}
SetConjuntos::SetConjuntos(const SetConjuntos &sc){
util = sc.util;
tam = sc.tam;
conjuntos = new Conjunto[tam];
for(int i = 0 ; i < util ; i++)
conjuntos[i]=sc.conjuntos[i];
}
SetConjuntos::~SetConjuntos(){
delete []conjuntos; //( NO cause segmetation fault )
}
SetConjuntos& SetConjuntos::operator=(const SetConjuntos &sc){
if(this != &sc){
util = sc.util;
tam = sc.tam;
Conjunto *conjuntos_loc = new Conjunto[tam];
for(int i = 0 ; i < util ; i++)
conjuntos_loc[i]=sc.conjuntos[i];
delete[] conjuntos;
conjuntos = conjuntos_loc;
}
return *this;
}
The code shown has no evident errors, so the mistake is in something you didn't place in the question.
As a wild guess you're (possibly involuntarily) using copy construction or assignments and this creates problems with the ownership.
Remember the rule: either you don't have a destructor, an assignment operator or a copy constructor or probably you will need all three of them.
This rules is so important that if you happen to meet a case in which you don't need a copy constructor but you need a destructor (can't think to one, but it could exist) please document it clearly with a comment that it's not something you forgot about.