I've encountered strange, unbelievable problem.
I wrote a program in which structure is reallocated several times.
The pointer is initially NULL
, and before allocation it is checked whether NULL
or it is deleted.
But, I've made a mistake here.
I wrote like this if (!pConfig) delete pConfig;
, which means it is never deleted.
I thought that the process will be terminated with exception, but in fact, it is never terminated with this and making memory leaks.
Pseudo code below:
Config *pConfig = NULL;
void func() {
if (!pConfig) delete pConfig; // <<<< Old wrong Code
// if (pConfig) delete pConfig; // <<<< Right Code
pConfig = new Config;
}
I used to know that deleting NULL
pointer makes a program to fault exit, for years, since I've learned in colledge.
But from this embarrassing fact, I've confused.
Anybody helps me to understand this as well? Thanks.
Maybe it will be duplicated with Deleting a null pointer