Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Why does this Curious Code work?

Why does this Curious Code work?

Scheduled Pinned Locked Moved The Lounge
questionc++help
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wolfram Steinke
    wrote on last edited by
    #1

    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!!

    M J 2 Replies Last reply
    0
    • W Wolfram Steinke

      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!!

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • W Wolfram Steinke

        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!!

        J Offline
        J Offline
        Jim Wuerch
        wrote on last edited by
        #3

        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."

        L 1 Reply Last reply
        0
        • J Jim Wuerch

          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."

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Actually, it is virtual. What this trick is trying to do (to the best of my understanding), is force the compiler to invoke the CWinApp::DoMessageBox virtual function directly instead of through the nonexistent v-table (NULL pointer).

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups