Why does this Curious Code work?
-
From Microsoft AppUI1.cpp int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp) { CWinApp* pApp = AfxGetApp(); if (pApp != NULL) return pApp->DoMessageBox(lpszText, nType, nIDHelp); else return pApp->CWinApp::DoMessageBox(lpszText, nType, nIDHelp); } We came across this code while fixing a bug where a in a TAPI call the message box reporting a failure crashed. On the surface of it this code would crash if pApp is NULL. Incidentally in our case it wasn't but invalid and hence the crash. OUCH!!! Happy programming!!
-
From Microsoft AppUI1.cpp int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp) { CWinApp* pApp = AfxGetApp(); if (pApp != NULL) return pApp->DoMessageBox(lpszText, nType, nIDHelp); else return pApp->CWinApp::DoMessageBox(lpszText, nType, nIDHelp); } We came across this code while fixing a bug where a in a TAPI call the message box reporting a failure crashed. On the surface of it this code would crash if pApp is NULL. Incidentally in our case it wasn't but invalid and hence the crash. OUCH!!! Happy programming!!
Because it's perfectly valid to call a method through a NULL pointer. The result is that, in the method,
this
is NULL. If you want to see something equally weird-looking, look at CWnd::GetSafeHwnd() --inline CWnd::GetSafeHwnd() const
{
return this == NULL ? NULL : m_hWnd;
}--Mike-- http://home.inreach.com/mdunn/ The preferred snack of 4 out of 5 Lounge readers.
-
From Microsoft AppUI1.cpp int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp) { CWinApp* pApp = AfxGetApp(); if (pApp != NULL) return pApp->DoMessageBox(lpszText, nType, nIDHelp); else return pApp->CWinApp::DoMessageBox(lpszText, nType, nIDHelp); } We came across this code while fixing a bug where a in a TAPI call the message box reporting a failure crashed. On the surface of it this code would crash if pApp is NULL. Incidentally in our case it wasn't but invalid and hence the crash. OUCH!!! Happy programming!!
If CWinApp::DoMessageBox isn't virtual, and it never dereferences the this pointer (never accesses any class members) then there's no problems. Essentially, it means that CWinApp::DoMessageBox could be static (it may be, I didn't look at it's definition to see) Jim Wuerch www.miwasoft.com Quote from my readme files: "This is BETA software, and as such may completely destroy your computer, change the alignment of the planets and invert the structure of the universe."
-
If CWinApp::DoMessageBox isn't virtual, and it never dereferences the this pointer (never accesses any class members) then there's no problems. Essentially, it means that CWinApp::DoMessageBox could be static (it may be, I didn't look at it's definition to see) Jim Wuerch www.miwasoft.com Quote from my readme files: "This is BETA software, and as such may completely destroy your computer, change the alignment of the planets and invert the structure of the universe."