Browse Source

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

Alex Szpakowski 8 years ago
parent
commit
b1b4a637b5

+ 1 - 1
src/modules/audio/openal/Audio.h

@@ -131,7 +131,7 @@ private:
 
 
 	public:
 	public:
 		PoolThread(Pool *pool);
 		PoolThread(Pool *pool);
-		~PoolThread();
+		virtual ~PoolThread();
 		void setFinish();
 		void setFinish();
 		void threadFunction();
 		void threadFunction();
 	};
 	};

+ 0 - 3
src/modules/thread/LuaThread.cpp

@@ -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
src/modules/thread/LuaThread.h

@@ -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
src/modules/thread/sdl/Thread.cpp

@@ -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
src/modules/thread/threads.h

@@ -76,7 +76,7 @@ private:
 	Mutex *mutex;
 	Mutex *mutex;
 };
 };
 
 
-class Threadable
+class Threadable : public love::Object
 {
 {
 public:
 public:
 	Threadable();
 	Threadable();

+ 2 - 2
src/modules/video/theora/Video.h

@@ -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();