C++ delete problem !!!

Started by
5 comments, last by Juksosah 18 years, 1 month ago
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.
Advertisement
You have a responsibility problem. Who is responsible for deleting the object? The list, or the creator of the object? Unless you have a clear responsibility policy, you will have bugs and there are no available workarounds for them, except for deciding who is responsible for deletion.

You could make it clear that the list must delete the object, for instance by storing auto_ptr's instead of plain pointers. This way, if someone tried to delete an object that is in the list, he is stupid.

As for testing NULL equality: pointers are independent. If a and b point to X, setting a to NULL has no effect on either b or X. Also note that deleting X and setting a to NULL still has no effect on b (which will be pointing to garbage memory). In fact, if you only have access to a and X, then you cannot do anything to tell b that it's pointing to incorrect memory.
There is no way in standard C++ to check if memory is deallocated (unless you keep track of allocations yourself).

The C++ Standard allows delete to set the pointer to 0, but in most cases it leave the pointer with the same value as when it was valid, therefore checking against 0 will only work on few compilers.

Quote:C++ Standard, 3.7.3.2.4
The effect of using an invalid pointer value (including passing it to a deallocation function) is undefined.


And a deleted pointer is invalid. So undefined behavior if you delete a already deleted pointer.

I think that you should assume the user is not stupid enough to delete memory you say you will delete.
It seems to me that if i am using a Linked List, i would not expect it to delete all of the items in it when i dont want to use the list anymore. Maybe the node i am putting into the Linked List is also being used by another object somewhere, and i wouldnt want the pointer deleted unexpectedly.

Maybe a solution would be that, when creating a Linked List you can pass into its constructor a boolean that tells it whether you want it to handle deleting memory or not. Maybe even give it a member function you can call to set that value. This way you get the best of both worlds, and you will know if the Linked List is going to free memory because you would have explicitly told it to.

[Edited by - Tesl on March 5, 2006 2:04:42 PM]
"Leave it to the computer programmers to shorten the "Year 2000 Millennium Bug" to "Y2K." Isn't that what caused this problem in the first place?"
Quote:Original post by Tesl
Maybe a solution would be that, when creating a Linked List you can pass into its constructor a boolean that tells it whether you want it to handle deleting memory or not. Maybe even give it a member function you can call to set that value. This way you get the best of both worlds, and if you will know if the Linked List is going to free memory because you would have explicitly told it to.


The canonical solution when deletion responsibilities are not black-or-white (list deletes nothing, or list deletes everything) or is to store smart pointers instead of pointers.

you can always just add a delete() method to your linked-list class. In implementation this would be similar to std::list< _Tp, _Alloc >::iterator erase().

http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/classstd_1_1list.html#std_1_1lista11
_____________________#define ever (;;)for ever delete (void *) rand();
Thanks a lot for the clarifications!!

It saved me a lot of time

This topic is closed to new replies.

Advertisement