diff --git a/indra/llcommon/llerror.cpp b/indra/llcommon/llerror.cpp
index a3053617d34f61acdd1217fe74112319b323da82..4f721fabdf06424c966254a1f50af7ae81892457 100755
--- a/indra/llcommon/llerror.cpp
+++ b/indra/llcommon/llerror.cpp
@@ -1357,15 +1357,9 @@ namespace LLError
 #endif
 
 	//static
-   void LLCallStacks::push(const char* function, const int line)
+   void LLCallStacks::allocateStackBuffer()
    {
-	   CallStacksLogLock lock;
-       if (!lock.ok())
-       {
-           return;
-       }
-
-	   if(!sBuffer)
+	   if(sBuffer == NULL)
 	   {
 		   sBuffer = new char*[512] ;
 		   sBuffer[0] = new char[512 * 128] ;
@@ -1375,6 +1369,31 @@ namespace LLError
 		   }
 		   sIndex = 0 ;
 	   }
+   }
+
+   void LLCallStacks::freeStackBuffer()
+   {
+	   if(sBuffer != NULL)
+	   {
+		   delete [] sBuffer[0] ;
+		   delete [] sBuffer ;
+		   sBuffer = NULL ;
+	   }
+   }
+
+   //static
+   void LLCallStacks::push(const char* function, const int line)
+   {
+	   CallStacksLogLock lock;
+       if (!lock.ok())
+       {
+           return;
+       }
+
+	   if(sBuffer == NULL)
+	   {
+		   allocateStackBuffer();
+	   }
 
 	   if(sIndex > 511)
 	   {
@@ -1406,15 +1425,9 @@ namespace LLError
            return;
        }
 
-	   if(!sBuffer)
+	   if(sBuffer == NULL)
 	   {
-		   sBuffer = new char*[512] ;
-		   sBuffer[0] = new char[512 * 128] ;
-		   for(S32 i = 1 ; i < 512 ; i++)
-		   {
-			   sBuffer[i] = sBuffer[i-1] + 128 ;
-		   }
-		   sIndex = 0 ;
+		   allocateStackBuffer();
 	   }
 
 	   if(sIndex > 511)
@@ -1445,11 +1458,9 @@ namespace LLError
            LL_INFOS() << " *************** END OF LL CALL STACKS *************** " << LL_ENDL;
        }
 
-	   if(sBuffer)
+	   if(sBuffer != NULL)
 	   {
-		   delete[] sBuffer[0] ;
-		   delete[] sBuffer ;
-		   sBuffer = NULL ;
+		   freeStackBuffer();
 	   }
    }
 
@@ -1459,5 +1470,10 @@ namespace LLError
        sIndex = 0 ;
    }
 
+   //static
+   void LLCallStacks::cleanup()
+   {
+	   freeStackBuffer();
+   }
 }
 
diff --git a/indra/llcommon/llerror.h b/indra/llcommon/llerror.h
index bc80e64423ba9ba5ab852ee6ff643c5a211de4b3..63040e1772a15b4a9239127df183e1b15a3a0df8 100755
--- a/indra/llcommon/llerror.h
+++ b/indra/llcommon/llerror.h
@@ -261,6 +261,9 @@ namespace LLError
    private:
        static char**  sBuffer ;
 	   static S32     sIndex ;
+
+	   static void allocateStackBuffer();
+	   static void freeStackBuffer();
           
    public:   
 	   static void push(const char* function, const int line) ;
@@ -268,6 +271,7 @@ namespace LLError
        static void print() ;
        static void clear() ;
 	   static void end(std::ostringstream* _out) ;
+	   static void cleanup();
    }; 
 }
 
diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp
index 2b634074d5969857de06b10213816a78a0d194d3..f92467a562051ee6265630609575d4de6bb892f1 100755
--- a/indra/newview/llappviewer.cpp
+++ b/indra/newview/llappviewer.cpp
@@ -2117,6 +2117,8 @@ bool LLAppViewer::cleanup()
 
 	ll_close_fail_log();
 
+	LLError::LLCallStacks::cleanup();
+
 	removeMarkerFiles();
 	
     LL_INFOS() << "Goodbye!" << LL_ENDL;