Using Memory Validator to detect when you are calling methods on objects that you’ve already deleted.
A common problem when writing C++ applications is the data corruption and/or application crash that is associated with using objects that have already been deleted. This error can happen when a dynamically allocated object is deleted, but the pointer to the object is not set to NULL, or there are multiple pointers to the object in the application and not all the pointers have been set to NULL. A simplified code fragment is shown below.
someObject *objPtr; // create the object objPtr = new someObject(); // simulate doing some work objPtr->readData(); callWorkerFunction1(objData); callWorkerFunction2(objData); objPtr->writeData(); // finished with the object delete objPtr; // forgot to set the pointer to NULL ... some more code // accidentally use the object, this is an error objPtr->calculateResults();
Often, the above code will lead to a crash at the point where the code is called. This is easy to debug.
What is not so easy to identify is when the call to calculateResults() doesn’t crash because the memory pointed to by objPtr is still a valid address. If calculateResults() modifies the memory pointed to by objPtr, one of three things can happen.
As you can see, identifying occasions when a deleted object is use is a very useful ability.
On the Deleted “this” tab of the settings dialog, select the Detect called functions for deleted C++ objects on the CRT heap check box. If you wish to see reports for NULL and invalid pointers, select the Report NULL “this” pointers and invalid”this” pointers check box.
exampleClass *ec; ec = new exampleClass(); delete ec; // call method on class object that has been destroyed - this is an error ec->exampleMethod();