Here's a simple problem for you gurus...
I got a linked list destructor that does destroys all the pointers allocated in it with new operator...
Util::LinkedList <AnyType *> list;
The destructor...
(in a while loop...)
delete head->object;
current = head;
head= head->next;
if (head!= NULL){
head->prev = NULL;
}
delete current;
I'm pretty sure it does not leave memory leaks. I tested it. But if I forgot sometin in my destructor just tell me.
Now the problem :
//Allocate dynamically the linked list, just for the purpose of the example
Util::LinkedList <AnyType *>* list = new Util::LinkedList <AnyType *>* ;
AnyType* fff = new AnyType();
AnyType* ggg = new AnyType();
//add fff and ggg in the list...
//Now I did something wrong..
delete fff;
//Delete the list, and all the stuff in it (fff and ggg)
delete list;
This crashes... because when the destructor of list tries to delete fff, it is aldready deleted. Two deletes for one new. I know it's the programmer's fault here, but it happens.
I would like the list destructor to handle this case gracefully... (exception, debug message etc...) Any suggestions ? thanks
I tried this, but for some reason it doesn't work... Can you explain me why ?
if (head->object == NULL){
error message
}
else{
delete head->object ;
}
Thank you
very sorry for the bad presentation of this message.