deleting an already deleted pointer

Started by
7 comments, last by Zahlman 18 years, 9 months ago
There is a bug in my code that crashes my computer upon exiting my application. I narrowed the functions that could be causing it down to a destructor. Now here's my question, if I have already deleted a pointer and I am trying to delete it again, will it crash the app(null-pointer or something?). Thanks in advance.
My Current Project Angels 22 (4E5)
Advertisement
Yes that would make it crash.
Thanks for the quick reply.
My Current Project Angels 22 (4E5)
It will cause because double-delete is a serious bug and you won't get a null pointer when you delete a pointer.

Think of pointers like the keys to your appartment in this regard, when you delete them you transfer ownership of them and the estate (the pointed to memory) to someone else (in this case the memory manager) and although you might have a spare key (you still have the pointer value) using it is not only immoral it's highly illegal and punishable (in this case by chrasing your app or spewing garbage over the heap).

edit: to darn slow...
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Of note is that deleting a NULLed out pointer is OK.

Bad:

foo * f = new foo;
delete f;
delete f;

Okay:

foo * f = new foo;
delete f;
f = 0;
delete f; //no effect
If you have a double-delete happening because of a destructor, then perhaps you have not provided an assignment operator (or copy constructor) which allocates and copies data in member pointer variables.
That caused a double-delete for me very recently.
I suggest you read up on the rule of three.
Quote:Any class that has an explicitly defined destructor, copy constructor or assignment operator generally needs all three.

btw, congrats Chris, on your article being top of the list in my Google search!
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
If you do need an assignment operator then don't forget to deallocate existing data in it, like that article does!
Array& operator= (const Array& a){	// delete the old data	delete[] oldData;	// do what we did before	size = a.size;	data = new int[size];	for(int i = 0 ; i < size ; ++i)	{		data = a.data;	}	return *this;}

Better expressed (with added exception safety) as:
Array& operator= (const Array& a){	// use the copy constructor to allocate memory for us	// avoids code duplication	Array tempCopy(a);	// would usually delagate to a swap() member function	std::swap(data, tempCopy.data);	std::swap(size, tempCopy.size);	// tempCopy's destructor calls delete[] on our old memory	return *this;}

Enigma
Enigma - your first version doesn't just have issues with exception-safety. It has problems with self assignment too. If you ever do foo = foo;, which may very well happen when you have a program filled with pointers, the object's internal data gets deleted before the 'copy' happens. Bad. The copy-swap idiom (your second version) is far superior, though it could be improved with a self-assignment check, for performance reasons, if you expect self-assignment to be common enough (frequency of self-assignment * cost of copy > cost of if statement), which is probably often the case (profile!!!).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
if you expect self-assignment to be common enough... which is probably often the case.


I think you missed a negation in there? o_O

This topic is closed to new replies.

Advertisement