Is KillTimer necessary? Is KillTimer necessary? windows windows

Is KillTimer necessary?


Timers set from HWNDs are implicitly destroyed by the window (hwnd) being destroyed. So no, you don't have to clean up your timers when the window exits.

But it's a good practice to have all your resources related to the window cleaned up on window close.


The timer will be destroyed automatically by Windows on process exit.

But bear in mind that (so it appears) your timer belongs to the window, not the process. So if your application allows these windows to be created and destroyed within a process, you'll be leaking timers.

It's always good practice to clean things up explicitly, because otherwise the lack of cleanup can come back to bite you later on.


According to MSDN, one should kill timers:

Applications should use the KillTimer function to destroy timers that are no longer necessary. The following example destroys the timers identified by the constants IDT_TIMER1, IDT_TIMER2, and IDT_TIMER3.

// Destroy the timers.
KillTimer(hwnd, IDT_TIMER1);
KillTimer(hwnd, IDT_TIMER2);
KillTimer(hwnd, IDT_TIMER3);

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644901(v=vs.85).aspx#creating_timer