CFrameWnd::AssertValid() fails in debug mode
-
I have Doc/View application. Upon one user command I create thread passing some user-defined data to it. This is the thread procedure: UINT ThreadLocalCopy(LPVOID pParam) { ___THREAD_COPY_PARAM* p_info = (THREAD_COPY_PARAM*)pParam; ___BYTE type = 0; ___DlgLocalCopy::LocalFolderCopy("", type, p_info); ___// finish operation ___::SendMessage(p_info->h_wnd, WM_THREADFINISHED, 0, 0); ___return 0; } And here's DlgLocalCopy::LocalFolderCopy() funcion, which is called from the thread above: void DlgLocalCopy::LocalFolderCopy(LPCTSTR pth, BYTE &type, THREAD_COPY_PARAM *p_param) { ___DlgOverwrite1 dov; ___DlgGetText dgt; ___DlgExpandSelection ds; ___CFileFind fff; ___CMainFrame* mf = (CMainFrame*) AfxGetApp()->GetMainWnd(); ___CMyDoc* p_doc = (CMyDoc*) mf->GetActiveDocument(); ___. ___. ___. } But calling CMyDoc* p_doc = (CMyDoc*) mf->GetActiveDocument() fails in CWnd::AssertValid() on this line: ASSERT((CWnd*)p == this); // must be us Why? I think that the stuff I am doing here is thread-safe, isn't it? I am not passing any CWnd objects to thread, I just call AfxGetApp()->GetMainWnd()->GetActiveDocument() inside the thread and it is not forbidden, is it? Or if it is, how to achieve it another (safe) way? Any help would be gratefully appreciated. Standa.
-
I have Doc/View application. Upon one user command I create thread passing some user-defined data to it. This is the thread procedure: UINT ThreadLocalCopy(LPVOID pParam) { ___THREAD_COPY_PARAM* p_info = (THREAD_COPY_PARAM*)pParam; ___BYTE type = 0; ___DlgLocalCopy::LocalFolderCopy("", type, p_info); ___// finish operation ___::SendMessage(p_info->h_wnd, WM_THREADFINISHED, 0, 0); ___return 0; } And here's DlgLocalCopy::LocalFolderCopy() funcion, which is called from the thread above: void DlgLocalCopy::LocalFolderCopy(LPCTSTR pth, BYTE &type, THREAD_COPY_PARAM *p_param) { ___DlgOverwrite1 dov; ___DlgGetText dgt; ___DlgExpandSelection ds; ___CFileFind fff; ___CMainFrame* mf = (CMainFrame*) AfxGetApp()->GetMainWnd(); ___CMyDoc* p_doc = (CMyDoc*) mf->GetActiveDocument(); ___. ___. ___. } But calling CMyDoc* p_doc = (CMyDoc*) mf->GetActiveDocument() fails in CWnd::AssertValid() on this line: ASSERT((CWnd*)p == this); // must be us Why? I think that the stuff I am doing here is thread-safe, isn't it? I am not passing any CWnd objects to thread, I just call AfxGetApp()->GetMainWnd()->GetActiveDocument() inside the thread and it is not forbidden, is it? Or if it is, how to achieve it another (safe) way? Any help would be gratefully appreciated. Standa.
MFC's handle maps are maintained on a per thread basis, so from your worker thread AfxGetApp()->GetMainWnd() won't work. One solution is to pass CMyDoc* to your thread creation function. Also it isn't a good idea to use SendMessage() from a thread, use PostMessage() instead or Events. Have a read of the Threading articles here on CP. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program