Sign in to follow this  
Juksosah

C++ delete problem !!!

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this