7

I know that a "deleting the same memory twice" error can happen when two pointers address the same dynamically allocated object. If delete is applied to one of the pointers, then the object’s memory is returned to the free store. If we subsequently delete the second pointer, then the free store may be corrupted.

But why doesn't this code cause a run-time error?

 string *str_1 = new string;
  auto str_2 = str_1;
  *str_1 = "AAA";
  cout<<*str_2<<endl;
  delete str_1;
  delete str_2;  // No Error

    // Prints AAA
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
Person.Junkie
  • 1,816
  • 4
  • 21
  • 31

5 Answers5

23

Deleting the same memory twice is undefined behaviour. Anything may happen, including nothing. It may e.g. cause a crash sometime later.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 2
    ...and because that's the point where certain people go cavalier ("hell, works for me, can't be that bad"), I usually add "...or it may format your hard drive". ;-) – DevSolar Aug 05 '14 at 12:00
  • 1
    Any ideas on how to debug if the program is crashing sometime later on? I faced this scenario and had to undo all my change before I found that I was deleting it twice. Would like to know if there is a better way to go about it. – user1198065 Aug 18 '14 at 16:58
  • Why is it undefined behavior? Does it just attempt to delete whatever's in the designated memory location? – cjsimon Feb 01 '18 at 18:54
  • 1
    @cjsimon Undefined behavior is usually about speed. Defining what happens would imply runtime checks that take CPU cycles to complete. A compiler is free to handle the situation any way it likes or not handle it at all (often the case, especially with optimization levels selected). It will most likely blindly update as if a valid object was there or crash fast as the code that keeps track of these things might assume delete is only ever called on pointers that came from new. – user904963 Mar 13 '22 at 02:36
2

I compiled this program in g++ 4.9.1 and it gave me a runtime error:

*** Error in `./t': free(): invalid pointer: 0xbfa8c9d4 ***

You are trying to free something which is already freed. Hence, the error.

shauryachats
  • 9,975
  • 4
  • 35
  • 48
2

I tried doing this in Visual Studio. There are two cases:

1)

delete p;
delete p;

This compiles properly but gives debug assertion failure when you run the program because you are trying to delete the memory location which is already deleted and no longer belongs to you.

2)

delete p;
p = NULL;
delete p;

This compiles properly and runs properly. There is no error. Try printing p before and after delete.

0

The behaviour of your program in not defined after the second delete command so you do not really know which error will be occured and printed in your terminal.

Nanc
  • 464
  • 5
  • 15
0

If you compile your program in Visual Studio Debug mode then the first delete should set the memory to 0xFEEEFEEE (see this question) - however the address is still accessible by your process so is unlikely to cause an exception. Linking in a product like BoundsChecker or Purify I believe checks for these byte patterns so that it can detect accessing of deleted memory.

Community
  • 1
  • 1
CraigJones
  • 61
  • 3