From: <sag...@us...> - 2011-08-21 21:21:55
|
Revision: 989 http://modplug.svn.sourceforge.net/modplug/?rev=989&view=rev Author: saga-games Date: 2011-08-21 21:21:48 +0000 (Sun, 21 Aug 2011) Log Message: ----------- [Imp] OpenMPT tries to close the audio driver and rescue unsaved work if an unhandled exception is encountered. Modified Paths: -------------- trunk/OpenMPT/mptrack/MainFrm.cpp trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/mptrack/Mptrack.h Modified: trunk/OpenMPT/mptrack/MainFrm.cpp =================================================================== --- trunk/OpenMPT/mptrack/MainFrm.cpp 2011-08-21 11:37:52 UTC (rev 988) +++ trunk/OpenMPT/mptrack/MainFrm.cpp 2011-08-21 21:21:48 UTC (rev 989) @@ -679,9 +679,11 @@ CMainFrame *pMainFrm = (CMainFrame *)theApp.m_pMainWnd; if (gbStopSent) return FALSE; BEGIN_CRITICAL(); - if ((pMainFrm) && (pMainFrm->IsPlaying()) && (CMainFrame::gpSoundDevice)) { + if ((pMainFrm) && (pMainFrm->IsPlaying()) && (CMainFrame::gpSoundDevice)) + { bOk = CMainFrame::gpSoundDevice->FillAudioBuffer(&gMPTSoundSource, gdwPlayLatency, dwUser); - }/* else { + }/* else + { CMainFrame::gpSoundDevice->SilenceAudioBuffer(&gMPTSoundSource, gdwPlayLatency, dwUser); }*/ if (!bOk) @@ -694,18 +696,6 @@ } -void Terminate_AudioThread() -//---------------------------------------------- -{ - //TODO: Why does this not get called. - AfxMessageBox("Audio thread terminated unexpectedly. Attempting to shut down audio device"); - CMainFrame* pMainFrame = CMainFrame::GetMainFrame(); - if (pMainFrame->gpSoundDevice) pMainFrame->gpSoundDevice->Reset(); - pMainFrame->audioCloseDevice(); - exit(-1); -} - - // Audio thread DWORD WINAPI CMainFrame::AudioThread(LPVOID) //------------------------------------------ @@ -714,7 +704,9 @@ BOOL bWait; UINT nSleep; - set_terminate(Terminate_AudioThread); +#ifdef WIN32 + ::SetUnhandledExceptionFilter(CTrackApp::UnhandledExceptionFilter); +#endif // WIN32 // -> CODE#0021 // -> DESC="use multimedia timer instead of Sleep() in audio thread" Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2011-08-21 11:37:52 UTC (rev 988) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2011-08-21 21:21:48 UTC (rev 989) @@ -1,3 +1,4 @@ +MB_ICONERROR // mptrack.cpp : Defines the class behaviors for the application. // @@ -587,6 +588,12 @@ #if (_MSC_VER >= 1400) _CrtSetDebugFillThreshold(0); // Disable buffer filling in secure enhanced CRT functions. #endif + + +#ifdef WIN32 + ::SetUnhandledExceptionFilter(UnhandledExceptionFilter); +#endif // WIN32 + m_pModTemplate = NULL; m_pPluginManager = NULL; m_bInitialized = FALSE; @@ -2967,6 +2974,54 @@ } +#ifdef WIN32 +// Try to close the audio device and rescue unsaved work if an unhandled exception occours... +LONG CTrackApp::UnhandledExceptionFilter(_EXCEPTION_POINTERS *) +//------------------------------------------------------------- +{ + CMainFrame* pMainFrame = CMainFrame::GetMainFrame(); + if(pMainFrame) + { + if(pMainFrame->gpSoundDevice) pMainFrame->gpSoundDevice->Reset(); + pMainFrame->audioCloseDevice(); + } + + // Rescue modified files... + CDocTemplate *pDocTmpl = theApp.GetModDocTemplate(); + if(pDocTmpl) + { + POSITION pos = pDocTmpl->GetFirstDocPosition(); + CDocument *pDoc; + + int fileID = 0; + const CString timeStamp = (CTime::GetCurrentTime()).Format("%Y%m%d.%H%M%S"); + + while((pos != NULL) && ((pDoc = pDocTmpl->GetNextDoc(pos)) != NULL)) + { + CModDoc *pModDoc = (CModDoc *)pDoc; + if(pModDoc->IsModified() && pModDoc->GetSoundFile() != nullptr) + { + fileID++; + CString filename; + filename.Format("%scrash_%s_%d.%s", theApp.GetConfigPath(), timeStamp, fileID, pModDoc->GetSoundFile()->GetModSpecifications().fileExtension); + pModDoc->OnSaveDocument(filename); + } + } + + if(fileID > 0) + { + CString message; + message.Format("A crash has been detected.\n%d modified file%s been rescued to %s", fileID, (fileID == 1 ? " has" : "s have"), theApp.GetConfigPath()); + ::MessageBox((pMainFrame ? pMainFrame->m_hWnd : NULL), message, "OpenMPT Crash", MB_ICONERROR); + } + } + + // Let Windows handle the exception... + return EXCEPTION_CONTINUE_SEARCH; +} +#endif // WIN32 + + ////////////////////////////////////////////////////////////////////////////////// // Localized strings Modified: trunk/OpenMPT/mptrack/Mptrack.h =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.h 2011-08-21 11:37:52 UTC (rev 988) +++ trunk/OpenMPT/mptrack/Mptrack.h 2011-08-21 21:21:48 UTC (rev 989) @@ -275,9 +275,16 @@ private: static void LoadRegistryDLS(); - #ifdef WIN32 // Legacy stuff +#ifdef WIN32 // Legacy stuff bool MoveConfigFile(TCHAR sFileName[_MAX_PATH], TCHAR sSubDir[_MAX_PATH] = "", TCHAR sNewFileName[_MAX_PATH] = ""); - #endif +#endif // WIN32 + +// Exception handling +#ifdef WIN32 +public: + static LONG WINAPI UnhandledExceptionFilter(_EXCEPTION_POINTERS *); +#endif // WIN32 + }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |