From 97a2171ea88fe52060e1dfe3fb09d2c320e1bb10 Mon Sep 17 00:00:00 2001
From: Graham Madarasz <graham@lindenlab.com>
Date: Tue, 4 Jun 2013 09:05:23 -0700
Subject: [PATCH] MAINT-2740 make use of OsOutputDebugString _DEBUG only to
 avoid interactions between Win 32-bit SEH and boost coroutine fiber stack
 handling

---
 indra/llcommon/llerror.cpp                   |  5 +----
 indra/llcommon/llerror.h                     | 14 ++++++++++++++
 indra/llcommon/llsys.cpp                     |  5 ++++-
 indra/llui/llxuiparser.cpp                   | 12 ++++--------
 indra/media_plugins/winmmshim/winmm_shim.cpp |  5 +++++
 5 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/indra/llcommon/llerror.cpp b/indra/llcommon/llerror.cpp
index 9b0141eb767..5c8e6cca290 100755
--- a/indra/llcommon/llerror.cpp
+++ b/indra/llcommon/llerror.cpp
@@ -201,10 +201,7 @@ namespace {
 		virtual void recordMessage(LLError::ELevel level,
 								   const std::string& message)
 		{
-			llutf16string utf16str =
-				wstring_to_utf16str(utf8str_to_wstring(message));
-			utf16str += '\n';
-			OutputDebugString(utf16str.c_str());
+			LL_WINDOWS_OUTPUT_DEBUG(wstring_to_utf16str(utf8str_to_wstring(message)));
 		}
 	};
 #endif
diff --git a/indra/llcommon/llerror.h b/indra/llcommon/llerror.h
index b65b4101531..08a5cd26df6 100755
--- a/indra/llcommon/llerror.h
+++ b/indra/llcommon/llerror.h
@@ -283,6 +283,20 @@ typedef LLError::NoClassInfo _LL_CLASS_TO_LOG;
 #define LL_ENDL llendl
 #define LL_CONT	(*_out)
 
+// Short story: We don't want to enable this in release builds.
+//
+// Long story: ...because this call generates C++ exceptions
+// which are handled and fine under the debugger, but instant death should they occur from
+// within a coroutine's stackframe due to inherent limitations of Windows 32-bit SEH
+// interacting with the fiber-based coroutine support used by boost.
+//
+// gmad BUG-2707/MAINT-2740
+#if LL_WINDOWS && defined(_DEBUG)
+	#define LL_WINDOWS_OUTPUT_DEBUG(a) OutputDebugString(utf8str_to_utf16str(a).c_str()), OutputDebugString("\n")
+#else
+	#define LL_WINDOWS_OUTPUT_DEBUG(a)
+#endif
+
 	/*
 		Use this construct if you need to do computation in the middle of a
 		message:
diff --git a/indra/llcommon/llsys.cpp b/indra/llcommon/llsys.cpp
index 57a6de9060c..418c5763f88 100755
--- a/indra/llcommon/llsys.cpp
+++ b/indra/llcommon/llsys.cpp
@@ -1385,7 +1385,7 @@ class FrameWatcher
             return false;
         }
         // Congratulations, we've hit a new low.  :-P
-
+#if _DEBUG
         LL_INFOS("FrameWatcher") << ' ';
         if (! prevSize)
         {
@@ -1398,6 +1398,9 @@ class FrameWatcher
         }
         LL_CONT << std::fixed << std::setprecision(1) << framerate << '\n'
                 << LLMemoryInfo() << LL_ENDL;
+#else
+	(void)prevSize;
+#endif
 
         return false;
     }
diff --git a/indra/llui/llxuiparser.cpp b/indra/llui/llxuiparser.cpp
index 3ad5ad7d425..4f3a2e06233 100755
--- a/indra/llui/llxuiparser.cpp
+++ b/indra/llui/llxuiparser.cpp
@@ -1311,8 +1311,7 @@ void LLXUIParser::parserWarning(const std::string& message)
 #ifdef LL_WINDOWS
 	// use Visual Studo friendly formatting of output message for easy access to originating xml
 	llutf16string utf16str = utf8str_to_utf16str(llformat("%s(%d):\t%s", mCurFileName.c_str(), mCurReadNode->getLineNumber(), message.c_str()).c_str());
-	utf16str += '\n';
-	OutputDebugString(utf16str.c_str());
+	LL_WINDOWS_OUTPUT_DEBUG(utf16str);
 #else
 	Parser::parserWarning(message);
 #endif
@@ -1322,8 +1321,7 @@ void LLXUIParser::parserError(const std::string& message)
 {
 #ifdef LL_WINDOWS
 	llutf16string utf16str = utf8str_to_utf16str(llformat("%s(%d):\t%s", mCurFileName.c_str(), mCurReadNode->getLineNumber(), message.c_str()).c_str());
-	utf16str += '\n';
-	OutputDebugString(utf16str.c_str());
+	LL_WINDOWS_OUTPUT_DEBUG(utf16str);
 #else
 	Parser::parserError(message);
 #endif
@@ -1642,8 +1640,7 @@ void LLSimpleXUIParser::parserWarning(const std::string& message)
 #ifdef LL_WINDOWS
 	// use Visual Studo friendly formatting of output message for easy access to originating xml
 	llutf16string utf16str = utf8str_to_utf16str(llformat("%s(%d):\t%s", mCurFileName.c_str(), LINE_NUMBER_HERE, message.c_str()).c_str());
-	utf16str += '\n';
-	OutputDebugString(utf16str.c_str());
+	LL_WINDOWS_OUTPUT_DEBUG(utf16str);
 #else
 	Parser::parserWarning(message);
 #endif
@@ -1653,8 +1650,7 @@ void LLSimpleXUIParser::parserError(const std::string& message)
 {
 #ifdef LL_WINDOWS
 	llutf16string utf16str = utf8str_to_utf16str(llformat("%s(%d):\t%s", mCurFileName.c_str(), LINE_NUMBER_HERE, message.c_str()).c_str());
-	utf16str += '\n';
-	OutputDebugString(utf16str.c_str());
+	LL_WINDOWS_OUTPUT_DEBUG(utf16str);
 #else
 	Parser::parserError(message);
 #endif
diff --git a/indra/media_plugins/winmmshim/winmm_shim.cpp b/indra/media_plugins/winmmshim/winmm_shim.cpp
index 47a1e5c0187..9e2e7ad3aa5 100755
--- a/indra/media_plugins/winmmshim/winmm_shim.cpp
+++ b/indra/media_plugins/winmmshim/winmm_shim.cpp
@@ -61,13 +61,18 @@ void ll_winmm_shim_initialize(){
 		{	// we have a dll, let's get out pointers!
 			initialized = true;
 			init_function_pointers(winmm_handle);
+#if defined(_DEBUG)
 			::OutputDebugStringA("WINMM_SHIM.DLL: real winmm.dll initialized successfully\n");
+#endif
 		}
+#if defined(_DEBUG)
 		else
 		{
 			// failed to initialize real winmm.dll
 			::OutputDebugStringA("WINMM_SHIM.DLL: Failed to initialize real winmm.dll\n");
 		}
+#endif
+
 	}
 	LeaveCriticalSection(&sCriticalSection);
 }
-- 
GitLab