You are here:   Home >> Win32/MFC >> Solution: Crash in Release build with self-defined message

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.

Permalink: Code Library » https://www.ucosoft.com/solution-crash-in-release-build-with-self-defined-message.html

Related Posts

One response to “Solution: Crash in Release build with self-defined message”

  1. bharath says:

    Thanks a lot, helped me..

Leave a Reply

Your email address will not be published. Required fields are marked *