Showing posts with label win32. Show all posts
Showing posts with label win32. Show all posts

CloseHandle vs. ReleaseMutex

Given that Win32 mutexes are "abandoned" when the owning thread exits without releasing them, you might make the mistake of assuming that closing a mutex handle without releasing it would have the same effect, but you'd be wrong. Suppose the following sequence of events:
  1. Process PA creates and owns named mutex M.
  2. Process PB "creates" a mutex by the same name. (GetLastError returns ERROR_ALREADY_EXISTS, so PB knows it didn't really create M, but that's beyond the scope of this discussion.)
  3. PA has done whatever it needed to, decides it is done with M, and closes its handle to M. M still exists, of course, because PB still has a handle to it. But M has just entered a "limbo" state in which it's useless to everyone and everything.
  4. PB attempts to "own" the mutex by passing it to WaitForSingleObject, and let's suppose for the sake of discussion that PB passes an INFINITE time-out to WaitForSingleObject.
Here's an alternate sequence of events:
  1. Process PA creates and owns named mutex M.
  2. Process PB "creates" a mutex by the same name. (GetLastError returns ERROR_ALREADY_EXISTS, so PB knows it didn't really create M, but that's beyond the scope of this discussion.)
  3. PB attempts to "own" the mutex by passing it to WaitForSingleObject, and let's suppose for the sake of discussion that PB passes an INFINITE time-out to WaitForSingleObject.
  4. PA has done whatever it needed to, decides it is done with M, and closes its handle to M. M still exists, of course, because PB still has a handle to it. But M has just entered a "limbo" state in which it's useless to everyone and everything.
In both sequences, PB is now stuck; WaitForSingleObject will never return. The only difference is that I expected PB to block in the second sequence after step 3 but then unblock after step 4. It's possible PA could "create" another handle to the mutex and pass the handle to ReleaseMutex, and I didn't try that, but it would be consistent with the notion that CloseHandle closes a handle to the mutex object, which exists in the kernel independently of the handle. Still, PA "creating" the handle again would be weird, and I expected CloseHandle to have the same effect as PA exiting, which was wrong.

endless WM_COMMAND/BN_CLICKED

When the user selects a radio button, the button sends a WM_COMMAND/BN_CLICKED notification message to the button's parent. This lets the program which owns the parent take action based on the new selection.

This works fine as long as nothing could possibly go wrong during said action, but of course that's almost never the case. Suppose the action involves allocating a lot of memory and the allocation fails; what then? A good program will know which radio button was set before the user clicked and restore that button (before or after informing the user of the failure).

Now the fun begins.

If the program restores the old radio button while processing the message, Windows* decides to send an identical message for that very same button as if the button had been clicked again. This doesn't happen right away; it's deferred in some tricky way, and believe me it's not worth trying to second-guess the deferral. A straightforward program is likely to find itself in an endless loop not of its own making. The user clicks the radio button, the program tries to allocate a bunch of memory, fails, restores the old radio button, Windows sends another message as if the user clicked the radio button again, the program tries to allocate a bunch of memory... and hilarity ensues.

The way out of this madness is to observe that the radio button is not set the second time the system informs the program that it was clicked. When the radio button is set, act; when it's not, don't. It's not intuitive that one must make this test, but one must.

* My testing was on Vista only.

mouse "moves" when it doesn't move

MSDN claims you'll receive WM_MOUSEMOVE when the mouse moves, and that's true as far as it goes. However, you'll also receive WM_MOUSEMOVE at a number of other times, notably when your window activates or another window moving reveals part of yours. So the real meaning of WM_MOUSEMOVE is "please act as if the mouse moved." That's fine most of the time, even though it means you might do some redundant work. But if you really want to know whether the mouse moved, you need to cache the last known mouse location, call GetMessagePos, and compare the result to your cached value. If the mouse really did move, then update your cache in addition to the other work you do.

Update 7 Dec 2006: St. Chen has already covered this topic. Big surprise.

tooltip message handling nightmare abated

I recently spent a boggling amount of time — a couple of weeks, on and off — second-guessing Microsoft's tooltips message handling code. Not only does it have at least one nasty bug, but it seems designed to make easy cases easy, which is a diplomatic way of saying it tends to make anything beyond that very difficult. Do try it, and see if it works for you, but if you spend more than a day scratching your head and trying to overcome limitations, my recommendation is to skip TTM_RELAYEVENT, turn off all tool flags other than TTF_TRACK and TTF_ABSOLUTE, and get real cozy with TTM_TRACKACTIVATE and TTM_TRACKPOSITION. Oh, and write your own equivalent of TTF_SUBCLASS. Once I realized I ought to give this approach a try, I was out of the woods in just a couple days, and I've written some C++ classes which will relieve me from ever having to deal with this madness again.