From 7a7f89db6d9c5e6b2c6c89ea39c0302907a0442b Mon Sep 17 00:00:00 2001
From: "Andrew A. de Laix" <alain@lindenlab.com>
Date: Thu, 11 Nov 2010 11:46:24 -0800
Subject: [PATCH] fix termination issues with thread safe queue in main loop
 repeater service.

---
 indra/llcommon/llthreadsafequeue.cpp |  6 +++---
 indra/newview/llappviewer.cpp        |  4 +++-
 indra/newview/llmainlooprepeater.cpp | 12 +++++++++---
 indra/newview/llmainlooprepeater.h   |  2 +-
 4 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/indra/llcommon/llthreadsafequeue.cpp b/indra/llcommon/llthreadsafequeue.cpp
index a7141605ef4..8a73e632a9a 100644
--- a/indra/llcommon/llthreadsafequeue.cpp
+++ b/indra/llcommon/llthreadsafequeue.cpp
@@ -53,13 +53,13 @@ LLThreadSafeQueueImplementation::LLThreadSafeQueueImplementation(apr_pool_t * po
 
 LLThreadSafeQueueImplementation::~LLThreadSafeQueueImplementation()
 {
-	if(mOwnsPool && (mPool != 0)) apr_pool_destroy(mPool);
 	if(mQueue != 0) {
 		if(apr_queue_size(mQueue) != 0) llwarns << 
-			"terminating queue which still contains elements;" << 
-			"memory will be leaked" << LL_ENDL;
+			"terminating queue which still contains " << apr_queue_size(mQueue) <<
+			" elements;" << "memory will be leaked" << LL_ENDL;
 		apr_queue_term(mQueue);
 	}
+	if(mOwnsPool && (mPool != 0)) apr_pool_destroy(mPool);
 }
 
 
diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp
index c06f0c18e87..76d518b6105 100644
--- a/indra/newview/llappviewer.cpp
+++ b/indra/newview/llappviewer.cpp
@@ -812,7 +812,7 @@ bool LLAppViewer::init()
 	}
 	
 	// Initialize the repeater service.
-	LLMainLoopRepeater::getInstance()->start();
+	LLMainLoopRepeater::instance().start();
 	
 	//
 	// Initialize the window
@@ -1737,6 +1737,8 @@ bool LLAppViewer::cleanup()
 		llinfos << "File launched." << llendflush;
 	}
 
+	LLMainLoopRepeater::instance().stop();
+
 	ll_close_fail_log();
 
     llinfos << "Goodbye!" << llendflush;
diff --git a/indra/newview/llmainlooprepeater.cpp b/indra/newview/llmainlooprepeater.cpp
index c2eba976419..ddc925a73b6 100644
--- a/indra/newview/llmainlooprepeater.cpp
+++ b/indra/newview/llmainlooprepeater.cpp
@@ -36,7 +36,7 @@
 
 
 LLMainLoopRepeater::LLMainLoopRepeater(void):
-	mQueue(gAPRPoolp, 1024)
+	mQueue(0)
 {
 	; // No op.
 }
@@ -44,6 +44,9 @@ LLMainLoopRepeater::LLMainLoopRepeater(void):
 
 void LLMainLoopRepeater::start(void)
 {
+	if(mQueue != 0) return;
+
+	mQueue = new LLThreadSafeQueue<LLSD>(gAPRPoolp, 1024);
 	mMainLoopConnection = LLEventPumps::instance().
 		obtain("mainloop").listen("stupid name here", boost::bind(&LLMainLoopRepeater::onMainLoop, this, _1));
 	mRepeaterConnection = LLEventPumps::instance().
@@ -55,13 +58,16 @@ void LLMainLoopRepeater::stop(void)
 {
 	mMainLoopConnection.release();
 	mRepeaterConnection.release();
+
+	delete mQueue;
+	mQueue = 0;
 }
 
 
 bool LLMainLoopRepeater::onMainLoop(LLSD const &)
 {
 	LLSD message;
-	while(mQueue.tryPopBack(message)) {
+	while(mQueue->tryPopBack(message)) {
 		std::string pump = message["pump"].asString();
 		if(pump.length() == 0 ) continue; // No pump.
 		LLEventPumps::instance().obtain(pump).post(message["payload"]);
@@ -73,7 +79,7 @@ bool LLMainLoopRepeater::onMainLoop(LLSD const &)
 bool LLMainLoopRepeater::onMessage(LLSD const & event)
 {
 	try {
-		mQueue.pushFront(event);
+		mQueue->pushFront(event);
 	} catch(LLThreadSafeQueueError & e) {
 		llwarns << "could not repeat message (" << e.what() << ")" << 
 			event.asString() << LL_ENDL;
diff --git a/indra/newview/llmainlooprepeater.h b/indra/newview/llmainlooprepeater.h
index 96b83b4916c..f84c0ca94c2 100644
--- a/indra/newview/llmainlooprepeater.h
+++ b/indra/newview/llmainlooprepeater.h
@@ -55,7 +55,7 @@ class LLMainLoopRepeater:
 private:
 	LLTempBoundListener mMainLoopConnection;
 	LLTempBoundListener mRepeaterConnection;
-	LLThreadSafeQueue<LLSD> mQueue;
+	LLThreadSafeQueue<LLSD> * mQueue;
 	
 	bool onMainLoop(LLSD const &);
 	bool onMessage(LLSD const & event);
-- 
GitLab