• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 783
  • Last Modified:

SendMessage occur access violation in Multithreaded Release version but do well in Debug version?

Hi all,
I write a testing program. I've uploaded it to my website http://www.webrobber.com/SendMessageST.rar 
It's based on multithread, a GUI thread and a work thread. They are very simple. There's a Button and an Edit control on the main window. Press the button start a new work thread.
void CSendMessageMTDlg::OnButton1()
{
     global.m_hWnd = GetSafeHwnd();
     AfxBeginThread(     Increase,0);
}
A work thread just increases a global variable and send a message to the main window to show the result. The function OnIncrease catches the message and shows it on the edit ctrl.
void CSendMessageMTDlg::OnIncrease()
{
     CString s;
     s.Format("%d",global.m_nNow);
     SetDlgItemText(IDC_EDIT1,s);
}
And the entire work thread lists below:
extern CTheGlobal global;
UINT Increase(LPVOID lParam){
  while(1){
    global.m_nNow ++;
    ::SendMessage(global.m_hWnd,IDM_ONINCREASE,0,0);
    Sleep(500);
  }
}
As I mentioned, the problem does well under the debug version, but cause access violation. Any one knows the reasons?
Best regards,
Wang
0
wang1st
Asked:
wang1st
1 Solution
 
jkrCommented:
Synchronize the access to the 'CTheGlobal' object, this is most likely to be the culprit. Use a critical section and an accessor method istead of incremeting public member variables directly:

void CTheGlobal::Increase () {

 EnterCriticalSection ( &m_cs);

 m_nNow++;

 LeaveCriticalSection ( &m_cs);
}

0
 
wang1stAuthor Commented:
But I lunched *only one* work thread, I dont think it's a synchro problem. Would you kind to download the source code of my problem at http://www.webrobber.com/SendMessageST.rar?
0
 
jkrCommented:
>>But I lunched *only one* work thread, I dont think it's a synchro problem

So you have at least 2 threads :o)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlexFMCommented:
Please give additional information about a place of access violation. Your dowmload link doesn't work.
0
 
vadikCommented:
This is very similar question:

http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20115128.html

I found it with yahoo.com, after I discovered a problem with the function AttachControlSite.

The solution are working just fine, I tried it. This is my code snippet. Sorry for my english...



HWND g_hWnd;
UINT gn_Now;

#define IDM_ONINCREASE WM_USER + 100

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
     //{{AFX_MSG_MAP(CMyDlg)
     ON_BN_CLICKED(IDC_MYBUTTON, OnMyButton)
     ON_MESSAGE(IDM_ONINCREASE, OnIncrease)
     //}}AFX_MSG_MAP
END_MESSAGE_MAP()

INT Increase(LPVOID lParam){
 while(1){
   gn_Now ++;

   ::SendMessage(g_hWnd,IDM_ONINCREASE,0,0);
   ::Sleep(500);
 }
}

void CMyDlg::OnMyButton()
{
    g_hWnd = GetSafeHwnd();
    AfxBeginThread(Increase,0);
}

afx_msg LRESULT CMyDlg::OnIncrease(WPARAM wParam, LPARAM lParam)
{
    CString s;
    s.Format("%d",gn_Now);
    SetDlgItemText(IDC_EDIT1,s);

    return S_OK;
}

----- mydlg.h ----

     //{{AFX_MSG(CTwoDlg)
     virtual BOOL OnInitDialog();
     afx_msg void OnPaint();
     afx_msg HCURSOR OnQueryDragIcon();
     afx_msg void OnMyButton();
     afx_msg LRESULT OnIncrease(WPARAM wParam, LPARAM lParam);
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()

etc....


0
 
wang1stAuthor Commented:
Accepted Answer from Zoppo  05/09/2001 12:43AM PST  
Maybe the message handler function declaration is wrong.

ON_MESSAGE( <message>, <function> )

needs a function declaration like
afx_msg LRESULT function(WPARAM, LPARAM);

if i.e. your message handler function's return type is 'void' this
may lead to a crash only in Release build (I don't know why,
but there's no problem in Debug build with this).


That's the answer, thank you vadik and all. To AlexFM , the source code is avaliable at http://www.webrobber.com/SendMessageST.rar
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now