What's wrong with this codes? But it generates Stack Overflow exception.
-
After initializing the timer by: SetTimer(ID_PLAYTIME_EVENT, 1000, NULL); The following codes will result in exception: 0XC00000FD: stack overflow? void CCPPPLAYERDlg::OnTimer (UINT nIDEvent) { MSG msg={0}; switch (nIDEvent) { case ID_PLAYTIME_EVENT: if(g_hwndMain) { // Main message loop while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } else { // AfxMessageBox(_T("Failed to create the main window!")); } break; } CDialog::OnTimer(nIDEvent); } DJ
-
After initializing the timer by: SetTimer(ID_PLAYTIME_EVENT, 1000, NULL); The following codes will result in exception: 0XC00000FD: stack overflow? void CCPPPLAYERDlg::OnTimer (UINT nIDEvent) { MSG msg={0}; switch (nIDEvent) { case ID_PLAYTIME_EVENT: if(g_hwndMain) { // Main message loop while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } else { // AfxMessageBox(_T("Failed to create the main window!")); } break; } CDialog::OnTimer(nIDEvent); } DJ
Your implementation is not corectly!! The return value of 'GetMessage' function can be: zero - If the function retrieves the WM_QUIT message; nonzero - If the function retrieves a message other than WM_QUIT; -1 - If there is an error (ex. if hWnd is an invalid window handle or lpMsg is an invalid pointer). In your function the 'GetMessage' returns nonzero always and when arrive a timer message this will be blocked in while loop, but after 1 sec. will arrive other timer message and will be blocked. This is the problem, you never exit from OnTimer function, but OnTimer function is called at each 1 sec. From this reason you have a Stack Overflow exception.
-
After initializing the timer by: SetTimer(ID_PLAYTIME_EVENT, 1000, NULL); The following codes will result in exception: 0XC00000FD: stack overflow? void CCPPPLAYERDlg::OnTimer (UINT nIDEvent) { MSG msg={0}; switch (nIDEvent) { case ID_PLAYTIME_EVENT: if(g_hwndMain) { // Main message loop while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } } else { // AfxMessageBox(_T("Failed to create the main window!")); } break; } CDialog::OnTimer(nIDEvent); } DJ
You are most likely right. The funciton (while uncommented) of AfxMessageBox(_T("Failed to create the main window!")); never be reached. Thus I called KillTimer somewhere in the program to kill ID_PLAYTIME_EVENT and there were no more Stack Overflow problems. But question again: How do the following codes work? Once this code being called and run, how does it exit the while loop? Is it the correct way to process the message queue? while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } From my tests, once the program runs to this code, the while loop will exit forever until exiting the entire program. Actually that is what I had expected so that some other codes can run on a multitasking windows such 2000. DJ