Visual C++ 6 - Release builds crash but Debug build do not crash

If you find that your Debug builds of a Visual C++ project work fine but your Release builds crash, check your message mappings.

If you've mapped a message to a handler that doesn't have the exact prototype (ish) that the message expects, your program will crash at run time. Or, rather, only the Release build of your program will crash at run time.

Look at this snippet of code:

    #define WM_MY_MESSAGE WM_USER+0x100
    ...
    ON_MESSAGE(WM_MY_MESSAGE, OnMyMessage)
    ...
    void SomeClass::OnMyMessage ( )
    {
	...
    }

We can see that it is obviously incorrect, because the message handler for ON_MESSAGE should accept two arguments, a WPARAM and an LPARAM. This is what the handler should look like:

    void SomeClass::OnMyMessage ( WPARAM wp, LPARAM lp )
    {
	...
    }

That's just one example of bad message maps. There are plenty more.

It's quite annoying that Visual C++ doesn't raise even a tiny warning if you get your message mappings wrong!

So, if your Release builds crash and your Debug builds work fine, double and triple check your message maps!

Update: 3/Mar/2005

Andy Kilgore wrote to say that a better way to "debug" crashes that only happen in release is to build the release build with debug symbols (which allows the exe to reference the debug info contained in a separate PDB file). This can be enabled as follows:

  1. Project | Settings ... : C/C++ tab: Category = General, Set Debug info to "Program Database"
  2. Project | Settings... : Link tab : Category = General Check "Generate debug info" Type "/OPT:REF" in the "Project Options" list box

Andy also mentioned that one might experiment with the /Gz compiler option to initialise memory as it would be initialized in debug mode.

Finally, Andy also sang the praises of John Robbin's most, most excellent books on Debugging Windows, whence this information came. The superlative classic version is out of print, but the dot net version is still a wonderful book. Whether you program for windows or not!