Browse Source

Changed getThreads so that the caller must allocate the memory.

rude 15 years ago
parent
commit
5fca0e9b33

+ 6 - 4
src/modules/thread/sdl/Thread.cpp

@@ -303,16 +303,18 @@ namespace sdl
 		return i->second;
 	}
 
-	Thread **ThreadModule::getThreads()
+	void ThreadModule::getThreads(Thread ** list)
 	{
-		Thread **list = new Thread*[threads.size()+1];
 		int c = 0;
 		for (threadlist_t::iterator i = threads.begin(); i != threads.end(); i++, c++)
 		{
 			list[c] = i->second;
 		}
-		list[threads.size()] = 0;
-		return list;
+	}
+
+	unsigned ThreadModule::getThreadCount() const
+	{
+		return threads.size();
 	}
 
 	void ThreadModule::unregister(const std::string & name)

+ 2 - 1
src/modules/thread/sdl/Thread.h

@@ -127,8 +127,9 @@ namespace sdl
 		ThreadModule();
 		virtual ~ThreadModule();
 		Thread *newThread(const std::string & name, love::Data *data);
-		Thread **getThreads();
+		void getThreads(Thread ** list);
 		Thread *getThread(const std::string & name);
+		unsigned getThreadCount() const;
 		void unregister(const std::string & name);
 		const char *getName() const;
 	}; // ThreadModule

+ 4 - 2
src/modules/thread/sdl/wrap_Thread.cpp

@@ -260,9 +260,11 @@ namespace sdl
 
 	int w_getThreads(lua_State *L)
 	{
-		Thread **list = instance->getThreads();
+		unsigned count = instance->getThreadCount();
+		Thread **list = new Thread*[count];
+		instance->getThreads(list);
 		lua_newtable(L);
-		for (int i = 0; list[i] != 0; i++)
+		for (unsigned int i = 0; i<count; i++)
 		{
 			luax_newtype(L, "Thread", THREAD_THREAD_T, (void*) list[i]);
 			list[i]->lock();