Free Source Code and Program Tips
Solution: Crash in Release build with self-defined message
We often meet such case: I have an application with a modeless dialog. The dialog comes up perfectly in DEBUG build, But in RELEASE build, it crashed.
Such problem often occures because of incorrect signature of message handle functions, that included into MFC message map for your CWnd based class.
We should very carefully check signatures such functions in comparision with expected one by MFC library. For example, if you put follow handler into message map
ON_MESSAGE(WM_SOME_USER_MESSAGE, OnMyMessage)
and define OnMyMessage as:
void CMyWnd::OnMyMessage()
{
//do something
}
this code will be compiled without errors. DEBUG build can work without any problems, but RELEASE will crash besause ON_MESSAGE macro expect follow
signature:
LRESULT CMyWnd::OnMyMessage(WPARAM /*wParam*/, LPARAM /*lParam*/)
DEBUG build works because stack processing has differences for debug and release builds.
so, the solution is simple:
Solution 1: modify the message handle functions with the following style and it will be ok.
void CMyWnd::OnMyMessage(WPARAM wParam, LPARAM lParam)
Solution 2: don’t modify the message handle functios, and change the ON_MESSAGE macro to ON_MESSAGE_VOID.
ON_MESSAGE_VOID(WM_SOME_USER_MESSAGE, OnMyMessage)
This macro is in AfxPriv.h, but is largely undocumented.
| Print article | This entry was posted by support on November 28, 2006 at 7:05 am, and is filed under Win32/MFC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No comments yet.
No trackbacks yet.
How to get ClistCtrl item text of another process
about 1 year ago - No comments
Question: In some app like WinSpy/Spy++ to make adjustments to list controls. It can correctly set an app’s list control’s modes, alignments, sorting, styles, and extended styles. It can also get the widths of the columns and count of items. The problem is that you cannot seem to get the columns’ names or the item
How to catch the events when click on the app icon on taskbar
about 1 year ago - No comments
How to catch the events when you click on the application’s icon on the taskbar? It’s the WM_NC class messages. For example, you can handle the WM_NCLBUTTONDBLCLK message. And you can alse reponsible the WM_SYSCOMMAND messages. In particular, the special cases of SC_MINIMIZE and SC_MAXIMIZE are what are called from the system menu, which is
How to send/post message to CDocument?
about 2 years ago - 2 comments
Sometime you need to send/post message to CDoumnet, but CDocument is not a window, and it can’t receive message. How do that? There are 3 solutions: Solution 1: Send the message to the main window. Send/Post the message to the main frame or the view, and then call some functions of the document. But if
Intermittent work in UI thread
about 2 years ago - No comments
Pedro Ferreira I’m trying to create a UI thread to do constant background work, but I’m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will be
Intermittent work in UI thread
about 2 years ago - No comments
Pedro Ferreira said: I’m trying to create a UI thread to do constant background work, but I’m having some design problems. The thread needs to fetch records from a database. do some processing on each record and, when there are no more records, sleep for 1 minute before check the database again. The process will
Simple CEdit Validation
about 3 years ago - No comments
Sometime you need some CEdit Validation, for example, restricting the data number only. The simple implemetion is to handle the EN_UPDATE notification message. The EN_UPDATE message is useful to handle as it caters for both normal entry and clipboard paste. You can do that in the Dialog or subclass the CEdit class. Here is an
How to translate among CString,string and char array
about 3 years ago - No comments
In MFC program, you’d better try to avoid using std::string. There is no compelling reason to use it in MFC, and it leads to confusion. There are some standard method to translate among Cstring ,string and char[]. // Suppose CString str; std::string s; char buf[SIZE]; // from CString to std::string s = str; // from
How to check whether the window is in minimise or Maximise mode
about 3 years ago - No comments
CWnd::IsIconic BOOL IsIconic( ) const; Return Value Nonzero if CWnd is minimized; otherwise 0. Remarks Specifies whether CWnd is minimized (iconic). Example // This code, normally emitted by the AppWizard for a dialog-based // project for the dialog’s WM_PAINT handler, runs only if the // window is iconic. The window erase the icon’s area, then
sscanf with CString
about 3 years ago - 3 comments
Problem: In the C time, we used to use sscanf to read data from string as following: char strUserName[20], strPassword[20]; // if the content of m_recvBuff is “Microsoft Bill”. sscanf((const char *)m_recvBuff, “%s %s”, strUserName, strPassword); And when you first get CString in sight, you may write the similar code: CString strUserName, strPassword; // if the
How to Draw Bitmap from a bmp File?
about 3 years ago - No comments
How to load a bitmap from bmp file and draw it in DC? There are two main steps: Load the bitmap from the file to a memory DC. Paint the content of the memory DC to the one which display the bitmap. In MFC, there is no API to load a bitmap directly from a