Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is KillTimer necessary?

I use SetTimer API and I see a lot of code like this:

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

Do I have to call KillTimer or the system will automatically free resources on the process exit? Does forgetting to call KillTimer lead to resource leaks?

I understand that if the timer is not needed it CAN be destroyed by KillTimer. But MUST it be destroyed manually?

like image 822
Sergey Podobry Avatar asked Jul 24 '09 11:07

Sergey Podobry


2 Answers

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.

like image 75
selbie Avatar answered Sep 23 '22 11:09

selbie


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.

like image 41
RichieHindle Avatar answered Sep 24 '22 11:09

RichieHindle