DEBUG ASSERTION FAILED ERROR

Hey all,

I have prepared a GUI for chatting application using winsock2 concept. Connection with server is created using GUI. However, I am facing a little issue. Once the server send some thing, it is received in buffer at client. I am putting the data from buffer to output window (which is a EDIT BOX on GUI) of client using SetDlgItemText(). However, as the compiler reach SetDlgItemText() an error is flashed:



DEBUG ASSERTION FAILED!
FILE: winocc.cpp
Line 138


By debugging I came to know that error appears when SetDlgItemText() is called. I went to winocc.cpp at line 138 as well. There also the same function was pointed out.


Part of file winocc.cpp around line 138 is also shown below:

1
2
3
4
5
6
7
8
9
10

void CWnd::SetDlgItemText(int nID, LPCTSTR lpszString)
{
	ASSERT(::IsWindow(m_hWnd));

	if (m_pCtrlCont == NULL)
		::SetDlgItemText(m_hWnd, nID, lpszString);
	else
		m_pCtrlCont->SetDlgItemText(nID, lpszString);
}


PART OF MY CODE IS GIVEN BELOW:

I have created GUI using MFC application wizard.
below is the code for OK button on GUI:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CGUI_chatDlg::OnOK() 
{
	// TODO: Add extra validation here
	unsigned int connect, disconnect;
	connect=IsDlgButtonChecked(IDC_RADIO1); //to connect to server
	disconnect=IsDlgButtonChecked(IDC_RADIO2);
	if(connect==1)
	{	
		client_program(); //Function where whole code to connect to server using winsock2 concepts.
	}

//	CDialog::OnOK();
}




Now code for client_program() is written below the above code:
Not listing the full code.

1
2
3
4
5
6
7
8
9
10
void client_program()
{
// all the code here

//connection to server

	sd=connect(cd,(sockaddr*)&client,client_length);
	_beginthread(Readthread,0,(void*)cd);  //Thread to read data from server.
}



Then read thread is defined below it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

void Readthread(void *parameter)
{
	
	CGUI_chatDlg c;
	char buff[512];
	
	unsigned int cd=(unsigned int)parameter;
	while(1)
	{
		
		
		if(recv(cd,buff,sizeof(buff),0)!=SOCKET_ERROR)  /*recv returns a positive value*/
		{	
			//cout<<endl<<"From Server:"<<buff<<flush;

			
			c.SetDlgItemText(IDC_EDIT2,buff);  // PROBLEM IS INVOKED HERE.
			memset(buff,0,512);
			//cout<<"\n\n"<<flush;
		}
		
	}
}


PROBLEM IS INVOKED at the place pointed in comment in Readthread.


Please help me guys. I am out of my mind with this error.

Regards,
Abhishek
Last edited on
Constructor code of CGUI_chatDlg? (in particular, the code related to window creation)?
I didnt get you.?
I think hanst99 is particulary interested in the constructor code for the CGUI_chatDlg class.

Guessing from the name - is it some dialog box derived from CDialog
OR is it some window class derived directly from CWnd ??

Either way, the actual window/dialog associated with this CGUI_chatDlg c; is not
being created correctly.
(The window probably need to be created with the Create member function )

But we need really the code to the CGUI_chatDlg class to see what the problem is.


On another issue - this CGUI_chatDlg c; variable would be local to the Readthread function and even if the window/dialog was created successfully it will only exist for a split second
before the function completed and the window was destroyed again.
gues, The function is a thread, I doubt it is supposed to quit before the program exits.
Oops - I didn't register the while(1) loop.
Hello,

I think CGUI_chatDlg class is created from CDialog class. Please find the constructor below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/////////////////////////////////////////////////////////////////////////////
// CGUI_chatDlg dialog

class CGUI_chatDlg : public CDialog
{
// Construction
public:
	CGUI_chatDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	//{{AFX_DATA(CGUI_chatDlg)
	enum { IDD = IDD_GUI_CHAT_DIALOG };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CGUI_chatDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CGUI_chatDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnConnect();
	afx_msg void OnDIsconnect();
	virtual void OnOK();
	afx_msg void OnChangeEdit2();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};



I was checking the definition of SetDlgItemText() on MSDN. I could find two definitions. One for windows and other for MFC. What is the difference between SetDlgItemText() Function (Windows) and CWnd::SetDlgItemText (MFC). I didnt get why two separate functions are used. Both take different parameters. I guess Readthread is defined outside the class, thus SetDlgItemText() for windows should be used. But I am not understanding the 3 parameters it will take.

Regards,
AbHI
Last edited on
Any idea guys.?
Please help me. I went on to debug further. Assertion fails on c.SetDlgItemText(). It does not get the handle of Windows in IsWindow() in winocc.cpp.
It's always a problem to call a function outside the thread where it is created. Especially MFC.

Here's an example how to do it:

http://www.codeguru.com/forum/showthread.php?t=312454
any help guys..???
Did you try the example coder gave?
Another solution would be to put all the comms stuff in a seperate UI thread.
Windows can create two types of threads:
1. Woker thread - this has no message pump - useful for background tasks

2. User Interface threads - has a message pump - useful if you need user interface elements (windows/dialog boxes, etc) in the thread


As you are using MFC - I suggest you look up CWinThread class



EDIT:
Running a dialog in modal mode should work in a worker thread because when run in a modal mode a dialog has it's own message pump.
Last edited on
Topic archived. No new replies allowed.