2006/01/18

-40

"When it's negative forty, it doesn't matter whether it is Fahrenheit or Celcius, it's same cold."

This is a joke Pei heard from her co-worker.

2006/01/09

[Visual C++]: PostMessage will give you different results in debug build and release build

Sometimes in VC, you need to program a message handler function to solve or go around with some other problems (in my case, the problem was the deadlock in threads, yuck...). So you can do that by register a window message and utilize it. For example, in *.h file you put this:

static const UINT UWM_MYMESSAGEFUNCTION = ::RegisterWindowMessage(_T("UWM_MYMESSAGEFUNCTION"));

and then, in message map you put this:

ON_REGISTERED_MESSAGE(UWM_MYMESSAGEFUNCTION, myMessageFunction)

and when you want to call the function, you can use:

PostMessage(UWM_MYMESSAGEFUNCTION);

By using this method, you can go around with some nasty issues (eg, with ugly threads deadlock issue).

However, sometimes, this will fail in release build, while working under debug build. Originally from msdn, PostMessage takes three parameters, and your message function should be something like:

LRESULT myMessageFunction(WPARAM wp, LPARAM lp)
{
//do something
return 0;
}

takes 2 parameters WPARAM & LPARAM and return something called LRESULT. Normally, by doing this, the error appeared in release build will be gone.

But, sometimes, this original message function is called by another message, for example, ON_COMMAND(), at this time it won't take any parameter. Again, if you use the above function override version, it still works in debug version, but in release version, windows will show you the error by showing its finger. So, you'd better override the original void function with the above one, so that windows can figure out by itself:


LRESULT myMessageFunction(WPARAM wp, LPARAM lp)
{
myMessageFucntion();
return 0;
}