Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

How to get return value of CreateDialog from CWinThread

$
0
0
Hello
I'm creating a dialog from thread then i need to get the return value from it.
Code:

UINT CSTC_Analiser::StockDiagram(LPVOID param)
{
        CSTC_Analiser* MainDialog = (CSTC_Analiser *)AfxGetApp()->m_pMainWnd;

        StStruct * _createstruct = (StStruct *)param;

        StockWindow m_pDialog;
        m_pDialog.m_strCaption = _createstruct->LineToSend;
        m_pDialog.Create(IDD_DIALOG1, MainDialog);
        m_pDialog.ShowWindow(SW_HIDE);

        SetEvent(MainDialog->Event_CreateWindow);

        MSG msg;
       
        while (GetMessage(&msg, NULL, 0, 0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
       
        delete _createstruct;  // Memory Leak here, is never called
        return msg.wParam;
}

The dialog works something then returns a value and i need to catch that. I create dialog from thread and wait for exit code
Code:

        StStruct  * _createstruct = new StStruct ;
        _createstruct->LineToSend = LineToSend;
        CWinThread * _create = AfxBeginThread(StockDiagram, _createstruct, THREAD_PRIORITY_NORMAL, NULL, CREATE_SUSPENDED);
        if (_create)
        {
                _create->m_bAutoDelete = FALSE;
                _create->ResumeThread();
        }

        WaitForSingleObject(_create->m_hThread, INFINITE);

        DWORD res;
        GetExitCodeThread(_create->m_hThread, &res);
        delete _create;

This code works fine but delete _createstruct; is never called causing a memory leak. From other dialog i use postquitmessage to send a code to thread:
Code:

BOOL StockWindow::OnInitDialog()
{
        CDialogEx::OnInitDialog();
            SetWindowText(m_strCaption);

        // do some work
        PostQuitMessage(777); // or other value depends on work

        return TRUE;
}

Any idea how to avoid memory leak ?
Thanks in advance!

Viewing all articles
Browse latest Browse all 3042

Trending Articles