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!

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.
Jean St-Arneault
PostMessage a WM_CLOSE to the HWND of the message box. You'd have to use FindWindow to get the HWND.
Jennifer Huang
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);
}