If the pointer is set to NULL
, a bug where the deleted object is used, for example through a dangling pointer to the object, might actually be hidden because the buggy code may check for NULL
before trying to use the data. In one way of thinking that might be consider 'safe' behavior, but what's happening is that you're actually hiding a defect that has already occurred.
Keep in mind that hiding bugs is not the same as fixing bugs. That dangling pointer might actually be used again after the memory has been reallocated, with a valid pointer placed into that same memory location. At that point the buggy code will start using the new, valid pointer, but for incorrect reasons.
So it actually might be better to set the pointer to something that will cause a crash if it's improperly used:
m_pchString = (char*) 0xdeaddead;
Now if something tries to use the member pointer of the deleted object (which is a bug), it'll fast fail and the bug will be caught rather than be hidden.
In debug builds With MSVC (and possibly other toolchains) you'll already get that behavior with the debug heap. The MSVC debug heap fills memory freed via free()
or operator delete
with the value 0xdd: https://stackoverflow.com/a/370362/12711