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

Posted on 2003-03-03
Medium Priority
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;
And the entire work thread lists below:
extern CTheGlobal global;
UINT Increase(LPVOID lParam){
    global.m_nNow ++;
As I mentioned, the problem does well under the debug version, but cause access violation. Any one knows the reasons?
Best regards,
Question by:wang1st
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
LVL 86

Expert Comment

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);


 LeaveCriticalSection ( &m_cs);


Author Comment

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?
LVL 86

Expert Comment

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)
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

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

Accepted Solution

vadik earned 800 total points
ID: 8060636
This is very similar question:


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;



INT Increase(LPVOID lParam){
   gn_Now ++;


void CMyDlg::OnMyButton()
    g_hWnd = GetSafeHwnd();

afx_msg LRESULT CMyDlg::OnIncrease(WPARAM wParam, LPARAM lParam)
    CString s;

    return S_OK;

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

     virtual BOOL OnInitDialog();
     afx_msg void OnPaint();
     afx_msg HCURSOR OnQueryDragIcon();
     afx_msg void OnMyButton();
     afx_msg LRESULT OnIncrease(WPARAM wParam, LPARAM lParam);



Author Comment

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

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