Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My mfc app crashes sometimes 1

Status
Not open for further replies.

RaKKeR

Programmer
Nov 22, 2004
26
NL
Hi,

I created a little app that just shows a modal dialog window with a message for the users to wait while the OS is installing devices. In the OnInitDialog function from the dialog window a thread is started which checks if there are pending install events and closes the dialog window when there are no more of these... It's at this point the app crashes sometimes. Here is some code to explain more in detail:

Code:
// USBWaitDlg.cpp : implementation file
//

#include "stdafx.h"
#include "USBWait.h"
#include "USBWaitDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//this is the thread that calls the waiting function
UINT CUSBWaitDlg::ThreadWaitInstalling(LPVOID params){
	CUSBWaitDlg * me = (CUSBWaitDlg *)params;
	me->WaitInstalling();
	return 0;

}

//this is the waiting function
void CUSBWaitDlg::WaitInstalling(){
	HINSTANCE hCfgMgr; //Handle to cfgmgr32.dll
	CMP_WaitNoPendingInstallEvents func;
	DWORD ret;

	hCfgMgr = LoadLibrary("C:\\windows\\system32\\cfgmgr32.dll");
	
	Sleep(1000);

	if(hCfgMgr != 0){
		func = (CMP_WaitNoPendingInstallEvents)GetProcAddress(hCfgMgr,"CMP_WaitNoPendingInstallEvents");
		ret = (*func)((DWORD)100);
		while(ret == WAIT_TIMEOUT){
			//AfxMessageBox("Busy installing",0,0);
			ret = (*func)((DWORD)100);
		}
	}
	EndDialog(IDOK); //I think the crash happens after this call...
}

CUSBWaitDlg::CUSBWaitDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUSBWaitDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUSBWaitDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

void CUSBWaitDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUSBWaitDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUSBWaitDlg, CDialog)
	//{{AFX_MSG_MAP(CUSBWaitDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CUSBWaitDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	AfxBeginThread(&ThreadWaitInstalling,(LPVOID)this);
	return TRUE;
}

void CUSBWaitDlg::OnPaint() 
{
	CDialog::OnPaint();
}

I hope someone sees what I'm doing wrong. Maybe the worker thread is not closed properly?

Thx in advance,

RaKKeR
 
That is not thread safe. You should never pass "this" to a thread. You can pass specific values in a struct, such as the HWND of the dialog, and use PostMessage to send thread-safe messages.
 
Thx for you reply timmay. Can you explain why passing the this pointer to a thread is not thread safe? I have seen alot of examples on the net doing the same. Imo that is not what is causing the problem...
 
When something is "not thread safe" is means that multiple threads can access the same variable at the same time, which pretty much is a no-no.

In your case it might be manipulating with flags (in the EndDialog call) that simultaneously is checked by the GUI thread.

In any case, you should follow timmay's advice. If nothing else to rule that out as the cause.

See faq116-5162 - "Threads in a GUI (MFC) app the easy way"




/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top