Skip to main content
GameDev.net gamedev.net
🔒 Locked

MFC KillTimer gives Assert Failure

Started by mmakrzem Aug 18, 2007 at 8:54 PM 0 replies 5.4k views
Original Post
mmakrzem
mmakrzem
I have a simple MFC dialog that starts a timer in the OnInitDialog() function by calling: m_uiTimer = SetTimer(WM_USER + 200, 1000, 0); my timer triggers void CViewDlg::OnTimer(UINT_PTR nIDEvent) { KillTimer(m_uiTimer); MessageBox("hi"); m_uiTimer = SetTimer(WM_USER + 200, 1000, 0); CDialog::OnTimer(nIDEvent); } //OnTimerand this works fine. However when I want to quit my application, I created a destructor that does this: CViewDlg::~CViewDlg() { if (m_uiTimer) { KillTimer( m_uiTimer ); } } when KillTimer in here is called I get an assert error: Debug Assertion Failed! ....bla bla bla.... \vs70builds\6030\vc\MFCATL\ship\atlmfc\include\afx win2.inl Line: 185 Anyone know what I may be doing wrong? I have also tried creating a separate "stopTimer()" function that I call when I am done with my dialog, but before CViewDlg is destroyed, but this also gives me the same assert error.
Endurion
Endurion
You have to know that in MFC a CWnd encapsulates a HWND. Both are not completely interchangeable. This means a CWnd can exist without an accompanying HWND.

Once the destructor is hit the HWND doesn't exist anymore. MFC asserts if you call a function which tries to access an invalid HWND.

Create an OnDestroy handler and put the KillTimer call there.

You might alternatively rely on Windows cleaning up the timer for you once the window gets destroyed automatically. But it doesn't feel as "clean" as the above approach.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.