Fix a potential crash when a Thread ends (resolves issue #1219).

This commit is contained in:
Alex Szpakowski
2016-10-08 10:50:50 -03:00
parent 304ccec71c
commit b1b4a637b5
6 changed files with 16 additions and 12 deletions
+1 -1
View File
@@ -131,7 +131,7 @@ private:
public: public:
PoolThread(Pool *pool); PoolThread(Pool *pool);
~PoolThread(); virtual ~PoolThread();
void setFinish(); void setFinish();
void threadFunction(); void threadFunction();
}; };
-3
View File
@@ -43,7 +43,6 @@ LuaThread::~LuaThread()
void LuaThread::threadFunction() void LuaThread::threadFunction()
{ {
this->retain();
error.clear(); error.clear();
lua_State *L = luaL_newstate(); lua_State *L = luaL_newstate();
@@ -83,8 +82,6 @@ void LuaThread::threadFunction()
if (!error.empty()) if (!error.empty())
onError(); onError();
this->release();
} }
bool LuaThread::start(const std::vector<Variant> &args) bool LuaThread::start(const std::vector<Variant> &args)
+2 -2
View File
@@ -36,12 +36,12 @@ namespace love
namespace thread namespace thread
{ {
class LuaThread : public love::Object, public Threadable class LuaThread : public Threadable
{ {
public: public:
LuaThread(const std::string &name, love::Data *code); LuaThread(const std::string &name, love::Data *code);
~LuaThread(); virtual ~LuaThread();
void threadFunction(); void threadFunction();
const std::string &getError() const; const std::string &getError() const;
+10 -3
View File
@@ -29,7 +29,7 @@ namespace sdl
Thread::Thread(Threadable *t) Thread::Thread(Threadable *t)
: t(t) : t(t)
, running(false) , running(false)
, thread(0) , thread(nullptr)
{ {
} }
@@ -74,9 +74,16 @@ bool Thread::isRunning()
int Thread::thread_runner(void *data) int Thread::thread_runner(void *data)
{ {
Thread *self = (Thread *) data; // some compilers don't like 'this' Thread *self = (Thread *) data; // some compilers don't like 'this'
self->t->retain();
self->t->threadFunction(); self->t->threadFunction();
Lock l(self->mutex);
self->running = false; {
Lock l(self->mutex);
self->running = false;
}
self->t->release();
return 0; return 0;
} }
} // sdl } // sdl
+1 -1
View File
@@ -76,7 +76,7 @@ private:
Mutex *mutex; Mutex *mutex;
}; };
class Threadable class Threadable : public love::Object
{ {
public: public:
Threadable(); Threadable();
+2 -2
View File
@@ -43,7 +43,7 @@ class Video : public love::video::Video
{ {
public: public:
Video(); Video();
~Video(); virtual ~Video();
// Implements Module // Implements Module
virtual const char *getName() const; virtual const char *getName() const;
@@ -58,7 +58,7 @@ class Worker : public love::thread::Threadable
{ {
public: public:
Worker(); Worker();
~Worker(); virtual ~Worker();
// Implements Threadable // Implements Threadable
void threadFunction(); void threadFunction();