?
Solved

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

Posted on 2003-03-03
6
Medium Priority
?
766 Views
Last Modified: 2013-11-20
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
Comment
Question by:wang1st
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 8057625
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
 

Author Comment

by:wang1st
ID: 8057683
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
 
LVL 86

Expert Comment

by:jkr
ID: 8057704
>>But I lunched *only one* work thread, I dont think it's a synchro problem

So you have at least 2 threads :o)
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 48

Expert Comment

by:AlexFM
ID: 8057824
Please give additional information about a place of access violation. Your dowmload link doesn't work.
0
 
LVL 1

Accepted Solution

by:
vadik earned 800 total points
ID: 8060636
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
 

Author Comment

by:wang1st
ID: 8061261
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question