Click here to Skip to main content
11,714,776 members (75,805 online)
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 18: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 Sergey Alexandrovich Kryukov 570
1 Richard MacCutchan 275
2 Maciej Los 274
3 OriginalGriff 140
4 CHill60 135
0 Sergey Alexandrovich Kryukov 545
1 CHill60 375
2 Maciej Los 324
3 Richard MacCutchan 310
4 OriginalGriff 260


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2015
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