C++ - Accessibility of object after deletion. (illegal still working), why this is happening?
-
In my project I created object for class using new operator. After use I deleted that object and set that to NULL. But even after deletion, I could able to access and able to run other functions using that (deleted)object. Please go through example: CTemp *objTemp = new CTemp; objTemp->Fun1(); delete objTemp; objTemp = NULL; objTemp->Fun2(); // It is illegal still works, why? Why this is happening?
-
In my project I created object for class using new operator. After use I deleted that object and set that to NULL. But even after deletion, I could able to access and able to run other functions using that (deleted)object. Please go through example: CTemp *objTemp = new CTemp; objTemp->Fun1(); delete objTemp; objTemp = NULL; objTemp->Fun2(); // It is illegal still works, why? Why this is happening?
-
In my project I created object for class using new operator. After use I deleted that object and set that to NULL. But even after deletion, I could able to access and able to run other functions using that (deleted)object. Please go through example: CTemp *objTemp = new CTemp; objTemp->Fun1(); delete objTemp; objTemp = NULL; objTemp->Fun2(); // It is illegal still works, why? Why this is happening?
In addition to the answer by CPallini: It will only work when your
CTemp::Fun2()
function does not access non-static member variables or call other member functions that would do so. Then the implicitthis
pointer will be used which isNULL
generating an access violation. -
In addition to the answer by CPallini: It will only work when your
CTemp::Fun2()
function does not access non-static member variables or call other member functions that would do so. Then the implicitthis
pointer will be used which isNULL
generating an access violation.