MessageBox memory leak/corruption problem

I have a main thread that displays a message box using CDialog's MessageBox function.

And there's another thread that exits the application.

Termination of the MessageBox (without the user pressing "OK") is corrupting memory or causing memory leaks.

In debug mode it says "user breakpoint is reached in code .. ".

Is there a simple way to close the messagebox before terminating the application so that there won't be memory corruption or memory leaks

Things I tried are:

* Using

pClassPointer->SendMessageToDescendants(WM_CLOSE, 0, 0, TRUE, FALSE);

This is not working! I do not know why!





Answer this question

MessageBox memory leak/corruption problem

  • Mrishi

    Hey it's suddenly working today!! I added SetWindowText(hWnd, "something"); and then I find the message box disappeared! Then I commented out all the PostMessage() calls and the message box remained. And then I uncommented one of the PostMessage(WM_CLOSE) calls and the message box disappeared! I don't know what's happenning. Until I added SetWindowText() command, the PostMessage(WM_CLOSE) didn't have any effect on the message box. And now I commented out SetWindowText() and it still works!

    Any idea why it's behaving so erratically

    And thanks for your help!

    Rajesh



  • Sean2408

    Are you sure the HWND is non-NULL and valid Try calling SetWindowText on the HWND and see if the title of the message box changes.

    rkonda wrote:

    Done! Doesn't work Nishant!

    Follows is the code. It is definitely getting the correct CWnd pointer because if I change the title string from "asdasd" to something else, it returns NULL. I tried using cWnd->PostMessage(WM_CLOSE); as well as ::PostMessage(hWnd, WM_CLOSE, NULL, NULL); and neither of them works! Any clues

    UINT MyControllingFunction( LPVOID pParam )
    {
    CMessageBoxTestDlg *pClassPointer;
    pClassPointer = (CMessageBoxTestDlg *) pParam;
    Sleep(2000);

    CWnd * cWnd = pClassPointer->FindWindow(NULL, "asdasd");
    // cWnd->PostMessage(WM_CLOSE);
    HWND hWnd = cWnd->GetSafeHwnd();
    ::PostMessage(hWnd, WM_CLOSE, NULL, NULL);
    // ::PostMessage(hWnd, WM_DESTROY, NULL, NULL);
    // pClassPointer->SendMessageToDescendants(WM_CLOSE, 0, 0, TRUE, FALSE);
    exit(0);
    }

    void CMessageBoxTestDlg::MessageBoxTest()
    {
    AfxBeginThread(MyControllingFunction, (void *) this);
    ::MessageBox(this->GetSafeHwnd(), "asad", "asdasd", MB_OK);
    }



  • Jean St-Arneault

    PostMessage a WM_CLOSE to the HWND of the message box. You'd have to use FindWindow to get the HWND.

    rkonda wrote:

    I have a main thread that displays a message box using CDialog's MessageBox function.

    And there's another thread that exits the application.

    Termination of the MessageBox (without the user pressing "OK") is corrupting memory or causing memory leaks.

    In debug mode it says "user breakpoint is reached in code .. ".

    Is there a simple way to close the messagebox before terminating the application so that there won't be memory corruption or memory leaks

    Things I tried are:

    * Using

    pClassPointer->SendMessageToDescendants(WM_CLOSE, 0, 0, TRUE, FALSE);

    This is not working! I do not know why!




  • Jennifer Huang

    In fact, this is not working properly either. Unless there is a Sleep(2) after PostMessage(WM_CLOSE), the MessageBox is not closing timely. So instead I used SendMessage(). Only calling DestroyWindow() on the CWnd * object returned by FindWindow() is working. In the sample program I wrote that is preventing memory dump. However in the other program it is still popping a message that "user breakpoint reached in code". Don't know what the problem is! Can a message be sent to the MessageBox to simulate "OK" button being pressed

  • laszlo.gosztola

    Done! Doesn't work Nishant!

    Follows is the code. It is definitely getting the correct CWnd pointer because if I change the title string from "asdasd" to something else, it returns NULL. I tried using cWnd->PostMessage(WM_CLOSE); as well as ::PostMessage(hWnd, WM_CLOSE, NULL, NULL); and neither of them works! Any clues

    UINT MyControllingFunction( LPVOID pParam )
    {
    CMessageBoxTestDlg *pClassPointer;
    pClassPointer = (CMessageBoxTestDlg *) pParam;
    Sleep(2000);

    CWnd * cWnd = pClassPointer->FindWindow(NULL, "asdasd");
    // cWnd->PostMessage(WM_CLOSE);
    HWND hWnd = cWnd->GetSafeHwnd();
    ::PostMessage(hWnd, WM_CLOSE, NULL, NULL);
    // ::PostMessage(hWnd, WM_DESTROY, NULL, NULL);
    // pClassPointer->SendMessageToDescendants(WM_CLOSE, 0, 0, TRUE, FALSE);
    exit(0);
    }

    void CMessageBoxTestDlg::MessageBoxTest()
    {
    AfxBeginThread(MyControllingFunction, (void *) this);
    ::MessageBox(this->GetSafeHwnd(), "asad", "asdasd", MB_OK);
    }



  • MessageBox memory leak/corruption problem