DestroyWindow

This was a fun Windows bug to catch. ("fun" is a euphemism for "not fun.")

Here's my pseudocode:

HWND g_hwnd = NULL;
main() {
  // Spin off a thread that discovers other people on the network.
  startDiscovery(myCallback);
  ... do the main work in my app ...
  // Cleanup.
  if (IsWindow(g_hwnd)) {
    DestroyWindow(g_hwnd);
  }
  assert(!IsWindow(g_hwnd));
}

myCallback() {
g_hwnd = CreateWindow("I found someone!!!");
}

Now, can you tell why the assertion fails?

The reason is that I created the window in a thread that was separate from the thread in which I tried to destroy it. The error returned by GetLastError() is ERROR_ACCESS_DENIED, and sure enough, MSDN documentation says "A thread cannot use DestroyWindow to destroy a window created by a different thread."

This was a hard bug to find! I think I'm going to change my callback to post a message to the main window. That ought to work instead.

Categories

About this Entry

This page contains a single entry by Mike Tsao published on April 15, 2004 10:21 PM.

VMware vs. Virtual PC was the previous entry in this blog.

SB 1822 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.2-en