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. Other Discussions
  3. Clever Code
  4. Super Subtle

Super Subtle

Scheduled Pinned Locked Moved Clever Code
helpc++data-structuresregexquestion
10 Posts 7 Posters 0 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.
  • R Offline
    R Offline
    realJSOP
    wrote on last edited by
    #1

    We've got some MFC code that was started when VC6 was still a good idea. This code is sprinkled with the obligatory use of BOOL (afterall, MFC likes the BOOL type). Today, I was running through our app, and noticed that when I clicked a button in the toolbar, the app would crash with the message     The stack around 'bCanClose' has become corrupted The code was hanging up on the following block of code:

    bool bCanClose = true
    HWND hWnd = m_pcDllView->GetSafeHwnd();
    if(IsWindow(hWnd))
    {
    AfxCallWndProc( m_pcDllView,
    hWnd,
    WM_QUERY_CLOSE, //messages.h #define WM_QUERY_CLOSE = WM_APP + 1
    (WPARAM) &bCanClose,
    0);
    if(! bCanClose) // <----------- Crash!
    {
    //...
    }
    }

    Here's the code that was handling the message being sent in the code shown above:

    LRESULT CMyFormView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch(message)
    {
    case WM_QUERY_CLOSE:
    *((BOOL*) wParam) = IsOkToClose();
    return 0;
    }

    return CFormView::WindowProc(message, wParam, lParam);
    

    }

    bool CMyFormView::IsOkToClose()
    {
    bool bResult = false;
    // ... some code
    return bResult;
    }

    Can anyone spot the problem? I saw it right away, but I think my spidey senses are working overtime because I'm in bug-fix mode. After I discovered this, I had two other equally experienced C++ programmers look at it, and they just shrugged their shoulders. The answer is in the wParam. When the message is sent, a BOOL is used. When the message is handled, it's expecting a bool, and its value is set accordingly. When the value is returned to the calling function, the stack is corrupted because the types in the casts don't match.

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

    R P R D A 5 Replies Last reply
    0
    • R realJSOP

      We've got some MFC code that was started when VC6 was still a good idea. This code is sprinkled with the obligatory use of BOOL (afterall, MFC likes the BOOL type). Today, I was running through our app, and noticed that when I clicked a button in the toolbar, the app would crash with the message     The stack around 'bCanClose' has become corrupted The code was hanging up on the following block of code:

      bool bCanClose = true
      HWND hWnd = m_pcDllView->GetSafeHwnd();
      if(IsWindow(hWnd))
      {
      AfxCallWndProc( m_pcDllView,
      hWnd,
      WM_QUERY_CLOSE, //messages.h #define WM_QUERY_CLOSE = WM_APP + 1
      (WPARAM) &bCanClose,
      0);
      if(! bCanClose) // <----------- Crash!
      {
      //...
      }
      }

      Here's the code that was handling the message being sent in the code shown above:

      LRESULT CMyFormView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
      {
      switch(message)
      {
      case WM_QUERY_CLOSE:
      *((BOOL*) wParam) = IsOkToClose();
      return 0;
      }

      return CFormView::WindowProc(message, wParam, lParam);
      

      }

      bool CMyFormView::IsOkToClose()
      {
      bool bResult = false;
      // ... some code
      return bResult;
      }

      Can anyone spot the problem? I saw it right away, but I think my spidey senses are working overtime because I'm in bug-fix mode. After I discovered this, I had two other equally experienced C++ programmers look at it, and they just shrugged their shoulders. The answer is in the wParam. When the message is sent, a BOOL is used. When the message is handled, it's expecting a bool, and its value is set accordingly. When the value is returned to the calling function, the stack is corrupted because the types in the casts don't match.

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      R Offline
      R Offline
      RodgerB
      wrote on last edited by
      #2

      Yeah I saw it right away too. This code is pretty ugly. Does this use MFC doc/view architecture because the document has much better ways of asking its views if its OK to close.

      R 1 Reply Last reply
      0
      • R realJSOP

        We've got some MFC code that was started when VC6 was still a good idea. This code is sprinkled with the obligatory use of BOOL (afterall, MFC likes the BOOL type). Today, I was running through our app, and noticed that when I clicked a button in the toolbar, the app would crash with the message     The stack around 'bCanClose' has become corrupted The code was hanging up on the following block of code:

        bool bCanClose = true
        HWND hWnd = m_pcDllView->GetSafeHwnd();
        if(IsWindow(hWnd))
        {
        AfxCallWndProc( m_pcDllView,
        hWnd,
        WM_QUERY_CLOSE, //messages.h #define WM_QUERY_CLOSE = WM_APP + 1
        (WPARAM) &bCanClose,
        0);
        if(! bCanClose) // <----------- Crash!
        {
        //...
        }
        }

        Here's the code that was handling the message being sent in the code shown above:

        LRESULT CMyFormView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
        {
        switch(message)
        {
        case WM_QUERY_CLOSE:
        *((BOOL*) wParam) = IsOkToClose();
        return 0;
        }

        return CFormView::WindowProc(message, wParam, lParam);
        

        }

        bool CMyFormView::IsOkToClose()
        {
        bool bResult = false;
        // ... some code
        return bResult;
        }

        Can anyone spot the problem? I saw it right away, but I think my spidey senses are working overtime because I'm in bug-fix mode. After I discovered this, I had two other equally experienced C++ programmers look at it, and they just shrugged their shoulders. The answer is in the wParam. When the message is sent, a BOOL is used. When the message is handled, it's expecting a bool, and its value is set accordingly. When the value is returned to the calling function, the stack is corrupted because the types in the casts don't match.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        I wish you'd left the answer off this one. I spotted it straight off, mainly because I was hosed by this back when I was still a real C++ developer. Oh well.:-D

        Deja View - the feeling that you've seen this post before.

        1 Reply Last reply
        0
        • R realJSOP

          We've got some MFC code that was started when VC6 was still a good idea. This code is sprinkled with the obligatory use of BOOL (afterall, MFC likes the BOOL type). Today, I was running through our app, and noticed that when I clicked a button in the toolbar, the app would crash with the message     The stack around 'bCanClose' has become corrupted The code was hanging up on the following block of code:

          bool bCanClose = true
          HWND hWnd = m_pcDllView->GetSafeHwnd();
          if(IsWindow(hWnd))
          {
          AfxCallWndProc( m_pcDllView,
          hWnd,
          WM_QUERY_CLOSE, //messages.h #define WM_QUERY_CLOSE = WM_APP + 1
          (WPARAM) &bCanClose,
          0);
          if(! bCanClose) // <----------- Crash!
          {
          //...
          }
          }

          Here's the code that was handling the message being sent in the code shown above:

          LRESULT CMyFormView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
          {
          switch(message)
          {
          case WM_QUERY_CLOSE:
          *((BOOL*) wParam) = IsOkToClose();
          return 0;
          }

          return CFormView::WindowProc(message, wParam, lParam);
          

          }

          bool CMyFormView::IsOkToClose()
          {
          bool bResult = false;
          // ... some code
          return bResult;
          }

          Can anyone spot the problem? I saw it right away, but I think my spidey senses are working overtime because I'm in bug-fix mode. After I discovered this, I had two other equally experienced C++ programmers look at it, and they just shrugged their shoulders. The answer is in the wParam. When the message is sent, a BOOL is used. When the message is handled, it's expecting a bool, and its value is set accordingly. When the value is returned to the calling function, the stack is corrupted because the types in the casts don't match.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          R Offline
          R Offline
          Rama Krishna Vavilala
          wrote on last edited by
          #4

          I may be missing something really obvious here, but I fail to understand why this will crash or corrupt the stack. sizeof(BOOL) = 4 sizeof(bool) = 1 So &bCanClose will point to 4 bytes of stack allocated memory. *((bool*)&bCanClose) will modify the first 1 byte of memory at the address pointed by bCanClose (which already has 4 bytes of allocated memory). So it should be safe.:confused: However the reverse will obviously cause a stack corruption.

          J R 2 Replies Last reply
          0
          • R Rama Krishna Vavilala

            I may be missing something really obvious here, but I fail to understand why this will crash or corrupt the stack. sizeof(BOOL) = 4 sizeof(bool) = 1 So &bCanClose will point to 4 bytes of stack allocated memory. *((bool*)&bCanClose) will modify the first 1 byte of memory at the address pointed by bCanClose (which already has 4 bytes of allocated memory). So it should be safe.:confused: However the reverse will obviously cause a stack corruption.

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            Thanks, I saw the difference in the code immediately. I just do not see why it should corrupt anything. :confused:

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            1 Reply Last reply
            0
            • R RodgerB

              Yeah I saw it right away too. This code is pretty ugly. Does this use MFC doc/view architecture because the document has much better ways of asking its views if its OK to close.

              R Offline
              R Offline
              realJSOP
              wrote on last edited by
              #6

              I don't know why this code was written the way it was. It's actually in a COM DLL.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              1 Reply Last reply
              0
              • R Rama Krishna Vavilala

                I may be missing something really obvious here, but I fail to understand why this will crash or corrupt the stack. sizeof(BOOL) = 4 sizeof(bool) = 1 So &bCanClose will point to 4 bytes of stack allocated memory. *((bool*)&bCanClose) will modify the first 1 byte of memory at the address pointed by bCanClose (which already has 4 bytes of allocated memory). So it should be safe.:confused: However the reverse will obviously cause a stack corruption.

                R Offline
                R Offline
                realJSOP
                wrote on last edited by
                #7

                Sorry - had the types switched. Now does it make sense?

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                J 1 Reply Last reply
                0
                • R realJSOP

                  We've got some MFC code that was started when VC6 was still a good idea. This code is sprinkled with the obligatory use of BOOL (afterall, MFC likes the BOOL type). Today, I was running through our app, and noticed that when I clicked a button in the toolbar, the app would crash with the message     The stack around 'bCanClose' has become corrupted The code was hanging up on the following block of code:

                  bool bCanClose = true
                  HWND hWnd = m_pcDllView->GetSafeHwnd();
                  if(IsWindow(hWnd))
                  {
                  AfxCallWndProc( m_pcDllView,
                  hWnd,
                  WM_QUERY_CLOSE, //messages.h #define WM_QUERY_CLOSE = WM_APP + 1
                  (WPARAM) &bCanClose,
                  0);
                  if(! bCanClose) // <----------- Crash!
                  {
                  //...
                  }
                  }

                  Here's the code that was handling the message being sent in the code shown above:

                  LRESULT CMyFormView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
                  {
                  switch(message)
                  {
                  case WM_QUERY_CLOSE:
                  *((BOOL*) wParam) = IsOkToClose();
                  return 0;
                  }

                  return CFormView::WindowProc(message, wParam, lParam);
                  

                  }

                  bool CMyFormView::IsOkToClose()
                  {
                  bool bResult = false;
                  // ... some code
                  return bResult;
                  }

                  Can anyone spot the problem? I saw it right away, but I think my spidey senses are working overtime because I'm in bug-fix mode. After I discovered this, I had two other equally experienced C++ programmers look at it, and they just shrugged their shoulders. The answer is in the wParam. When the message is sent, a BOOL is used. When the message is handled, it's expecting a bool, and its value is set accordingly. When the value is returned to the calling function, the stack is corrupted because the types in the casts don't match.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  D Offline
                  D Offline
                  Doc Lobster
                  wrote on last edited by
                  #8

                  One never stops learning! Anyway, compiler flag /Zp4 should solve this easily! :-D

                  1 Reply Last reply
                  0
                  • R realJSOP

                    Sorry - had the types switched. Now does it make sense?

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    Now that makes since! Thanks for clearing up the confusion. :-D

                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                    1 Reply Last reply
                    0
                    • R realJSOP

                      We've got some MFC code that was started when VC6 was still a good idea. This code is sprinkled with the obligatory use of BOOL (afterall, MFC likes the BOOL type). Today, I was running through our app, and noticed that when I clicked a button in the toolbar, the app would crash with the message     The stack around 'bCanClose' has become corrupted The code was hanging up on the following block of code:

                      bool bCanClose = true
                      HWND hWnd = m_pcDllView->GetSafeHwnd();
                      if(IsWindow(hWnd))
                      {
                      AfxCallWndProc( m_pcDllView,
                      hWnd,
                      WM_QUERY_CLOSE, //messages.h #define WM_QUERY_CLOSE = WM_APP + 1
                      (WPARAM) &bCanClose,
                      0);
                      if(! bCanClose) // <----------- Crash!
                      {
                      //...
                      }
                      }

                      Here's the code that was handling the message being sent in the code shown above:

                      LRESULT CMyFormView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
                      {
                      switch(message)
                      {
                      case WM_QUERY_CLOSE:
                      *((BOOL*) wParam) = IsOkToClose();
                      return 0;
                      }

                      return CFormView::WindowProc(message, wParam, lParam);
                      

                      }

                      bool CMyFormView::IsOkToClose()
                      {
                      bool bResult = false;
                      // ... some code
                      return bResult;
                      }

                      Can anyone spot the problem? I saw it right away, but I think my spidey senses are working overtime because I'm in bug-fix mode. After I discovered this, I had two other equally experienced C++ programmers look at it, and they just shrugged their shoulders. The answer is in the wParam. When the message is sent, a BOOL is used. When the message is handled, it's expecting a bool, and its value is set accordingly. When the value is returned to the calling function, the stack is corrupted because the types in the casts don't match.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      A Offline
                      A Offline
                      Anna Jayne Metcalfe
                      wrote on last edited by
                      #10

                      Spotted it right away. I must have spent way too much time stepping through message map code (those sort of return value mismatches could happen all too easily with VC6) before I jumped onto the WTL bandwagon. :doh:

                      Anna :rose: Linting the day away :cool: Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"

                      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