Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VS2010
Hi
 
I have got a problem with destructor calls.
 
I have a class which implements a linked list like this:
 
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
 
//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
 
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
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:
 
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 31-Oct-12 19:18pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
MyList foo = new MyList;
(foo->lstGetList1())->vdPrint();
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try to add const to either inGetNodeData or lstGetList1 and see if it makes difference
  Permalink  

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

  Print Answers RSS
0 Mathew Soji 330
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 216
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100