One of the problems with MFC programming that often causes trouble for programmers is the broken message map. The symptoms of a broken message map are the application works perfectly in Debug mode, but in release mode the application crashes with a corrupt stack at an apparently random point after choosing a menu option or interacting with a user interface control.
Programmers often look elsewhere for the problem becuase of the corrupt stack and the release mode nature of the bug. You can spend a lot of time looking for uninitialised variables when the real problem is an incorrectly coded message map.
The problem occurs when a function referred to in a message map has the incorrect number of parameters compared to the number of parameters expected by the message map handling macro. For example, a menu item selection would normally expect a function with no parameters such as:
void OnColourRed();
and a context menu handler may expect a function with the following prototype:
void OnContextMenu(CWnd *wnd, CPoint point);
If the function specified in the message map has the wrong number of parameters (too many or not enough) the stack will not have the expected alignment. For various reasons, in debug code, this error gets fixed up. However in release mode the error does not get fixed. When the function gets called, assuming the function doesn’t crash because of the incorrect parameters, when the function returns, the wrong amount of data is popped from the stack, prior to calling the return instruction. When the return instruction executes, it pulls the return address from the stack at the wrong address and returns to the random address pulled off the stack. The application usually crashes at this point, with a corrupt stack.
Memory Validator has settings that automatically detect when a function with the wrong signature (parameter description) is called in a message map. This functionality works in both debug and release mode. To enable the message map hooks, select Check MFC message maps for correct parameter passing on the MFC Message Map Checks tab.