The Deleted "this" Pointer tab provides controls for determining how some deleted object hooks are installed relating to track functions called for deleted, NULL, or invalid 'this' pointers.
The default settings are shown below:
Calling functions that belong to a deleted object is a common error, especially if different parts of the application are holding the object pointer.
Memory Validator can check the this pointer of every C++ class member function to ensure that it is:
•not a pointer to a deallocated object
•not NULL
•does not point to a memory address that is not accessible
These checks are not enabled by default because it can significantly affect performance speed and memory usage.
Note that uninitialised data detection is disabled when this feature is enabled
•Detect calling of functions for deleted C++ objects on the CRT heap enables detection of functions being called for deleted C++ objects
When enabled you can additionally check for the following:
•Report NULL this pointers and Invalid this pointers enables detection of functions being called for NULL and invalid this pointers
An Invalid this pointer is defined as one that does not point at the stack, memory in any DLL, or any other allocated memory.
You may want to see NULL this pointers for the specific MFC functions designed to receive NULL pointers ( see list below ):
•Report NULL this pointers for MFC functions designed to receive NULL this pointers enables the relevant reporting (disabled by default)
This option is not generally recommended as you will typically see many such reports. However, there may be uses for this in specific cases, and we like to give you the option!
When a deleted object has a function called and has been detected, the display in the Memory tab shows an entry to indicate where the error occurred.
The trace can be expanded to show the callstack and the source code.
In the example shown here, the ex->exampleMethod() has just been called, which has triggered the error report.
Some examples follow, and a list of MFC functions that are designed to be called with NULL this pointers.
Two examples are illustrated below which demonstrate common scenarios in which these errors occur.
Example 1 - calling a function for a deleted object:
Consider the following much simplified example code:
myObj *obj;
// create an object, do some work and then destroy the object
obj = new myObj();
obj->doWork();
delete obj;
// at this point 'obj' is no longer valid,
... application does some more work
// another part of the application incorrectly uses the object that was deleted earlier on (because
// the pointer wasn't reset to NULL, or more than one copy of the pointer existed).
obj->doSomeMoreWork();
There is no guarantee that the call to doSomeMoreWork() will actually crash, which would otherwise alert you to the error.
If the memory pointed to by obj is still a valid memory address, then:
•reads of data will read undefined values
•writes may corrupt existing data, or
•writes may write to memory locations that will never be accessed by the application again, or that may be overwritten by any following memory allocation.
Enabling the features outlined above will help detect these errors.
Example 2 - MFC functions designed for NULL this pointers:
Consider the following snippet of example code that demonstrates an error:
CWnd *wnd;
wnd = getMyWindow(); // function that should return a valid window, but sometimes returns NULL;
wnd->GetSafeHwnd();
The above code looks unsafe, as there is the potential to call GetSafeHwnd() with a NULL this pointer.
However, if you examine the MFC source or documentation for CWnd::GetSafeHwnd() you will see that this function is designed to allow it to be called with a NULL this pointer. See just below for a list of such functions.
As described above, Memory Validator allows you to detect functions being called with NULL this pointers, whilst also allowing you to ignore the selected MFC functions that are designed to be called with NULL this pointers.
•CHandleMap::DeleteTemp
•CImageList::GetSafeHandle
•CGdiObject::operator HGDIOBJ
•CGdiObject::GetSafeHandle
•CPen::operator HPEN
•CBrush::operator HBRUSH
•CFont::operator HFONT
•CBitmap::operator HBITMAP
•CPalette::operator HPALETTE
•CRgn::operator HRGN
•CDC::operator HDC
•CDC::GetSafeHdc
•CMenu::operator HMENU
•CMenu::GetSafeHmenu
•CWnd::operator HWND
•CWnd::GetSafeHwnd
•CWinThread::operator HANDLE
•CWinApp::DoMessageBox
•CDC::DPtoHIMETRIC
•CDC::HIMETRICtoDP
•CWnd::AttachControlSite
•CWnd::GetParentFrame
•CWnd::GetTopLevelParent
•CWnd::GetTopLevelOwner
•CWnd::GetParentOwner
•CWnd::GetTopLevelFrame
•CPlex::FreeDataChain
Reset All - Resets all global settings, not just those on the current page.
Reset - Resets the settings on the current page.