I've had this same issue crop up twice in the last couple months. This last time I spent two days trying to find this bug (it was in some older code I had written).
So I figured I pass this along. Maybe it will save someone some trouble in the future.
MFC uses macros to bind handlers (functions) to messages. So for example if you want to handle WM_ERASEBKGRND in your CWnd, you would add an OnEraseBkgrnd member function to your class, and specify the ON_WM_ERASEBKGND() macro inside the BEGIN_MESSAGE_MAP ... END_MESSAGE_MAP block. The ON_WM_ERASEBKGND() macro registers your function (name must be OnEraseBkgrnd) with the MFC framework so it is called whenever a WM_ERASEBKGND occurs.
Now normally that is handled for you by ClassWizard, which provides a nice GUI to select what messages you want to handle. However it often happens that ClassWizard doesn't know about your class, especially when you insert new source files into your project, or share source across multiple projects.
What screwed me up big-time was that my function was not properly declared. Yet the ON_WM_ERASEBKGND() macro happily registered the function with the MFC framework without any type checking.
BOOL OnEraseBkgnd(CDC *pDC) is the expected return type and parameters. I was using BOOL OnEraseBkgnd(). This led to a really nasty, hard to isolate bug causing 12 bytes of memory to be overwritten (first 12 bytes of the class) in release build for x86 (but not ARM or debug builds).
So the lesson is, if you manually add new handlers to a class be absolutely certain your functions are properly declared. You will not get compilation errors if they are wrong.
Dan East
So I figured I pass this along. Maybe it will save someone some trouble in the future.
MFC uses macros to bind handlers (functions) to messages. So for example if you want to handle WM_ERASEBKGRND in your CWnd, you would add an OnEraseBkgrnd member function to your class, and specify the ON_WM_ERASEBKGND() macro inside the BEGIN_MESSAGE_MAP ... END_MESSAGE_MAP block. The ON_WM_ERASEBKGND() macro registers your function (name must be OnEraseBkgrnd) with the MFC framework so it is called whenever a WM_ERASEBKGND occurs.
Now normally that is handled for you by ClassWizard, which provides a nice GUI to select what messages you want to handle. However it often happens that ClassWizard doesn't know about your class, especially when you insert new source files into your project, or share source across multiple projects.
What screwed me up big-time was that my function was not properly declared. Yet the ON_WM_ERASEBKGND() macro happily registered the function with the MFC framework without any type checking.
BOOL OnEraseBkgnd(CDC *pDC) is the expected return type and parameters. I was using BOOL OnEraseBkgnd(). This led to a really nasty, hard to isolate bug causing 12 bytes of memory to be overwritten (first 12 bytes of the class) in release build for x86 (but not ARM or debug builds).
So the lesson is, if you manually add new handlers to a class be absolutely certain your functions are properly declared. You will not get compilation errors if they are wrong.
Dan East