Click here to Skip to main content
15,885,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have got a problem with destructor calls.

I have a class which implements a linked list like this:

C++
template <class NODETYPE>
class List
{
public:
	List();
	~List();
	void vdInsertFront(const NODETYPE &);
	void vdInsertLast(const NODETYPE &);
	int inDeleteFront(void);
	int inDeleteLast(void);
	bool blIsEmpty();
	void vdPrint();
	int vdShuffle(void);
	int inGetLenght();
	NODETYPE * inObtenerDatoNodo(int);

private:
	int inLenght;
	Node<NODETYPE> *ptrFirst;
	Node<NODETYPE> *ptrLast;
	Node<NODETYPE> *ptrNewNode(const NODETYPE &);
};


This is its constructor and destructor

C++
//Constructor
template <class NODETYPE>
List<NODETYPE>::List()
{
	ptrFirst = 0;
	ptrLast = 0;
}

//Destructor
template <class NODETYPE>
List<NODETYPE>::~List()
{
	if(!blEmpty())
	{
		Nodo<NODETYPE> * ptrActual = ptrFirst;
		Nodo<NODETYPE> * ptrAux;

		while(ptrActual != 0)
		{
			ptrAux = ptrActual;
			ptrActual = ptrActual->ptrNext;
			delete ptrAux;
		}
	}
}

//Some functions bodies
.
.
.

This function returns a pointer to an especific element of the list

C++
template <class NODETYPE>
NODETYPE * List<NODETYPE>::inGetNodeData(int index)
{
	Nodo<NODETYPE> * ptrAux = ptrFirst;
	for(int i = 0; i <= index - 1; i++)
		ptrAux = ptrAux->ptrNext;

	return &ptrAux->_data;
}


Now I have another class that has a list object as a private member and
a function that returns a pointer of the list
C++
class MyClass
{
public:
	List<int> * lstGetList1(void);
private:
	List<int> lstList;	
};

List<int> * foo::lstGetList1(void)
{
	return &lstList;
}


Now, if I call the function vdPrint of the member list in a foo object like this:

MyClass foo;
(foo.lstGetList1())->vdPrint();



The destructor is called, deleting my list when I don't want to do that.

The code in this example is very simplified of what I have. In fact I'm creating a list of MyClass objects and the the code is more or less like this:

C++
List<MyClass> foo;

//Inserting MyClass objects to foo...

((foo.inGetNodeData(1))->lstGetList1())->vdPrint();


I think the destructor its been called because Im creating an "anonymous" list object, calling vdPrint method, an then, because the next line code is beyond the anonymous list scope, the destructor is called automatically. How can I avoid this?


Thanks in advance

Ivan
Posted

vc
MyClass foo;
(foo.lstGetList1())->vdPrint();


Calling vdPrint() will not delete your list.
Here you created a local object of MyClass.

I hope implementation of vdPrint() is to print the elements, and it will not destroy the object.

lstGetList1() returns the address of List.
inGetNodeData also returns the adress of List.

But foo is a local object, and it will be destroyed after its scope.
Please try to allocate object of MyClass in heap and destroy it whenever not needed.

C++
MyList foo = new MyList;
(foo->lstGetList1())->vdPrint();
 
Share this answer
 
v2
try to add const to either inGetNodeData or lstGetList1 and see if it makes difference
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900