Is KillTimer required?

I use the SetTimer API, and I see a lot of codes like this:

case WM_DESTROY: // Destroy the timer. KillTimer(hwnd, IDT_TIMER); PostQuitMessage(0); break; 

Should I call KillTimer, or will the system automatically free resources when I exit the process? Does forgetting to call KillTimer lead to a resource leak?

I understand that if a timer is not needed, it can be destroyed by KillTimer. But MUST be destroyed manually?

+6
windows winapi timer
source share
3 answers

Timers set from HWND are implicitly destroyed when a window is destroyed (hwnd). This way, no, you do not need to clear your timers when the window comes out.

But itโ€™s good practice to have all your window-related resources cleaned up when you close the window.

+7
source share

The timer will be automatically destroyed by Windows at the end of the process.

But keep in mind that (it seems) your timer belongs to a window, not a process. Therefore, if your application allows you to create and destroy these windows in the process, you will skip timers.

It is always useful to clean things properly, because otherwise, lack of cleaning can bite you again.

+4
source share

According to MSDN, you need to kill timers:

Applications should use the KillTimer function to kill timers that are no longer needed. The following example destroys 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

0
source share

All Articles