From b1b4a637b500cd5cf105244f8d21a47fe0afa7f8 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 8 Oct 2016 10:50:50 -0300 Subject: [PATCH] Fix a potential crash when a Thread ends (resolves issue #1219). --- src/modules/audio/openal/Audio.h | 2 +- src/modules/thread/LuaThread.cpp | 3 --- src/modules/thread/LuaThread.h | 4 ++-- src/modules/thread/sdl/Thread.cpp | 13 ++++++++++--- src/modules/thread/threads.h | 2 +- src/modules/video/theora/Video.h | 4 ++-- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/modules/audio/openal/Audio.h b/src/modules/audio/openal/Audio.h index b33731e23..4ec6d4d63 100644 --- a/src/modules/audio/openal/Audio.h +++ b/src/modules/audio/openal/Audio.h @@ -131,7 +131,7 @@ private: public: PoolThread(Pool *pool); - ~PoolThread(); + virtual ~PoolThread(); void setFinish(); void threadFunction(); }; diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 4c463ecfe..d7150a31a 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/src/modules/thread/LuaThread.cpp @@ -43,7 +43,6 @@ LuaThread::~LuaThread() void LuaThread::threadFunction() { - this->retain(); error.clear(); lua_State *L = luaL_newstate(); @@ -83,8 +82,6 @@ void LuaThread::threadFunction() if (!error.empty()) onError(); - - this->release(); } bool LuaThread::start(const std::vector &args) diff --git a/src/modules/thread/LuaThread.h b/src/modules/thread/LuaThread.h index a3ce4729a..169dce6f6 100644 --- a/src/modules/thread/LuaThread.h +++ b/src/modules/thread/LuaThread.h @@ -36,12 +36,12 @@ namespace love namespace thread { -class LuaThread : public love::Object, public Threadable +class LuaThread : public Threadable { public: LuaThread(const std::string &name, love::Data *code); - ~LuaThread(); + virtual ~LuaThread(); void threadFunction(); const std::string &getError() const; diff --git a/src/modules/thread/sdl/Thread.cpp b/src/modules/thread/sdl/Thread.cpp index b5bde2992..f775f7481 100644 --- a/src/modules/thread/sdl/Thread.cpp +++ b/src/modules/thread/sdl/Thread.cpp @@ -29,7 +29,7 @@ namespace sdl Thread::Thread(Threadable *t) : t(t) , running(false) - , thread(0) + , thread(nullptr) { } @@ -74,9 +74,16 @@ bool Thread::isRunning() int Thread::thread_runner(void *data) { Thread *self = (Thread *) data; // some compilers don't like 'this' + self->t->retain(); + self->t->threadFunction(); - Lock l(self->mutex); - self->running = false; + + { + Lock l(self->mutex); + self->running = false; + } + + self->t->release(); return 0; } } // sdl diff --git a/src/modules/thread/threads.h b/src/modules/thread/threads.h index 190aa0170..0039f7086 100644 --- a/src/modules/thread/threads.h +++ b/src/modules/thread/threads.h @@ -76,7 +76,7 @@ private: Mutex *mutex; }; -class Threadable +class Threadable : public love::Object { public: Threadable(); diff --git a/src/modules/video/theora/Video.h b/src/modules/video/theora/Video.h index 4eba5888e..05ed06848 100644 --- a/src/modules/video/theora/Video.h +++ b/src/modules/video/theora/Video.h @@ -43,7 +43,7 @@ class Video : public love::video::Video { public: Video(); - ~Video(); + virtual ~Video(); // Implements Module virtual const char *getName() const; @@ -58,7 +58,7 @@ class Worker : public love::thread::Threadable { public: Worker(); - ~Worker(); + virtual ~Worker(); // Implements Threadable void threadFunction();