From 07f97739e1b97ac01e53e8d91b966c3461f9ffe1 Mon Sep 17 00:00:00 2001 From: "Matias N. Goldberg" Date: Wed, 20 Mar 2024 15:55:45 -0300 Subject: [PATCH] Do not blanket-catch all exceptions from worker threads Unhandled exceptions should crash the process, not unexpectedly stop the thread. --- OgreMain/include/Threading/OgreThreads.h | 40 +++++++++++++----------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/OgreMain/include/Threading/OgreThreads.h b/OgreMain/include/Threading/OgreThreads.h index 791a67e9943..ab0ad8ca68e 100644 --- a/OgreMain/include/Threading/OgreThreads.h +++ b/OgreMain/include/Threading/OgreThreads.h @@ -41,6 +41,21 @@ THE SOFTWARE. # define OGRE_THREAD_CALL_CONVENTION #endif +namespace Ogre +{ + template + struct DeleteOnDestructor + { + T *ptr; + DeleteOnDestructor( T *_ptr ) : ptr( _ptr ) {} + ~DeleteOnDestructor() { delete ptr; } + + // Prevent being able to copy this object + DeleteOnDestructor( const DeleteOnDestructor & ) = delete; + DeleteOnDestructor &operator=( const DeleteOnDestructor & ) = delete; + }; +} // namespace Ogre + #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WINRT /// See Threads::CreateThread for an example on how to use # define THREAD_DECLARE( threadFunction ) \ @@ -48,15 +63,9 @@ THE SOFTWARE. { \ unsigned long retVal = 0; \ Ogre::ThreadHandle *threadHandle( reinterpret_cast( argName ) ); \ - try \ - { \ - threadHandle->_setOsHandleToSelf(); \ - retVal = threadFunction( threadHandle ); \ - } \ - catch( ... ) \ - { \ - } \ - delete threadHandle; \ + Ogre::DeleteOnDestructor container( threadHandle ); \ + threadHandle->_setOsHandleToSelf(); \ + retVal = threadFunction( threadHandle ); \ return retVal; \ } #else @@ -66,16 +75,9 @@ THE SOFTWARE. { \ unsigned long retVal = 0; \ Ogre::ThreadHandle *threadHandle( reinterpret_cast( argName ) ); \ - try \ - { \ - threadHandle->_setOsHandleToSelf(); \ - retVal = threadFunction( threadHandle ); \ - } \ - catch( ... ) \ - { \ - } \ - delete threadHandle; \ -\ + Ogre::DeleteOnDestructor container( threadHandle ); \ + threadHandle->_setOsHandleToSelf(); \ + retVal = threadFunction( threadHandle ); \ return (void *)retVal; \ } #endif