-1

I have a node class that contains 3 node pointers and 2 integers. I allocate all the nodes with new, but when I call delete on them, the integers get set to -17891602 and it's screwing up the rest of my code's boundary checking. What would cause delete to do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    Can you share your code. – Avinash Nov 03 '11 at 04:56
  • 1
    Without seeing some code it's hard to know. Maybe you have a bug in your destructor(s)? – Laurence Gonsalves Nov 03 '11 at 04:57
  • Why are you checking the memory contents after you called `delete` on it? You are not supposed to do that.`delete` does not guarantee to write anything specific at memory locations.Many implementations of delete do intentionally write specific garbage to help detect mistakes like accessing the memory after calling `delete`. – Alok Save Nov 03 '11 at 05:12

3 Answers3

5

After a delete that memory is not yours any more. Don't inspect it, don't do anything with it, because if you do then you have Undefined Behavior. It will likely soon be reused.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Just to add, many compilers, in debug builds, will deliberately write garbage to freed memory to make it easier to catch you accessing stuff you shouldn't have. – Nicol Bolas Nov 03 '11 at 04:59
  • It seems to me that yemathums is saying that deleting pointer members of a node changes the integer members. He is not inspecting deleted memory (as far as he knows). – Don Reba Nov 03 '11 at 05:46
0

Check whether the pointer fields also get new values. Are they equal to 0xfeeefeee? (That's -17891602 in hexadecimal.) Your memory manager might be overwriting freed memory so it's easier to recognize in crash dumps when you attempt to read or write memory you're not supposed to access anymore.

If you're reading freed objects to do bounds checking, then you're relying on undefined behavior. Check the documentation for your environment to find out what, if anything, it does with freed memory. Your bounds checker will need to co-operate with it; you cannot assume it will work in the general case.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
0

If you develop on Linux and use gdb you could put a watchpoint using the watch command to GDB. This can help finding when was a memory location overwritten.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547