May 31st, 2012

What happens if I call KillTimer with a NULL hwnd?

A customer discovered a bug in their code and wanted some information on how serious it was, so they could assess how urgently they need to issue a fix.

We have code that calls Set­Timer with a valid window handle, but then we destroy the window before we get around to calling Kill­Timer. When we finally do call Kill­Timer, we do so with a NULL window handle. The Kill­Timer calls are probably harmless, but are we leaking the timers?

The customer’s real concern was actually in the part of the problem they thought was a point of little concern. The window manager cleans up orphaned timers when the associated window is destroyed, so there is no timer leak in this case. Of course, it’s still good practice to clean up those timers. (Note however that a similar situation does lead to leaked timers.) The real danger is in the Kill­Timer call. By passing a null window handle, you are killing a thread timer. Maybe you’re lucky and there is no thread timer whose ID is the value you passed as the second parameter, but someday your luck will run out and you will accidentally kill somebody else’s timer.

The customer was pleased with this explanation.

That’s exactly the information we were looking for. Thanks.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.