Index: trunk/psLib/src/sys/psError.c
===================================================================
--- trunk/psLib/src/sys/psError.c	(revision 9730)
+++ trunk/psLib/src/sys/psError.c	(revision 11265)
@@ -11,9 +11,13 @@
  *  @author Eric Van Alst, MHPCC
  *
- *  @version $Revision: 1.41 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-10-24 22:52:56 $
+ *  @version $Revision: 1.42 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-01-24 22:14:48 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
  */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include <stdarg.h>
@@ -55,7 +59,7 @@
     if (psArrayLength(errorStack) < MAX_ERROR_STACK_SIZE) {
         // make the psErr persistent
-        p_psMemSetPersistent(err, true);
-        p_psMemSetPersistent(err->msg, true);
-        p_psMemSetPersistent(err->name, true);
+        psMemSetPersistent(err, true);
+        psMemSetPersistent(err->msg, true);
+        psMemSetPersistent(err->name, true);
 
         psArrayAdd(errorStack, 0, err);
@@ -85,6 +89,6 @@
         // allocate the error stack
         errorStack = psArrayAllocEmpty(MAX_ERROR_STACK_SIZE);
-        p_psMemSetPersistent(errorStack, true);
-        p_psMemSetPersistent(errorStack->data, true);
+        psMemSetPersistent(errorStack, true);
+        psMemSetPersistent(errorStack->data, true);
         // store this threads error stack
         // note that pthread_setspecifc() does not take a pointer as the first
@@ -93,4 +97,26 @@
             psAbort(__func__, "pthread_setspecific() failed");
         }
+    }
+
+    return errorStack;
+}
+
+// This function serves the same purpose of psErrorStackGet() accept it does
+// not alloc the error stack if it DOES NOT already exist.  Unlike
+// psErrorStackGet(), this function is NOT guarenteed to return a valid pointer
+// and it's return status must be checked.
+static psArray *psErrorStackGetNoAlloc(void)
+{
+    // check to see if the error stack key has been initialized
+    pthread_mutex_lock(&lockErrorStack);
+    if (errorStackKeyInitialized == false) {
+        return NULL;
+    }
+    pthread_mutex_unlock(&lockErrorStack);
+
+    // check to see if the error stack for this thread has been allocated
+    psArray *errorStack = NULL;
+    if ((errorStack = pthread_getspecific(errorStack_key)) == NULL) {
+        return NULL;
     }
 
@@ -237,7 +263,7 @@
         psErr *err = errorStack->data[i];
 
-        p_psMemSetPersistent(err, false);
-        p_psMemSetPersistent(err->msg, false);
-        p_psMemSetPersistent(err->name, false);
+        psMemSetPersistent(err, false);
+        psMemSetPersistent(err->msg, false);
+        psMemSetPersistent(err->name, false);
     }
 
@@ -260,7 +286,14 @@
 }
 
+// This function does not allocate any memory so it is safe to call from inside
+// of psMemory.c.  Do not allocate memory in function (or call any functions
+// that do) without first removing it's use from psMemory.c.
 void psErrorStackPrintV(FILE *fd, const char *format, va_list va)
 {
-    psArray *errorStack = psErrorStackGet();
+    psArray *errorStack = psErrorStackGetNoAlloc();
+    // do nothing if the error stack has not been allocated
+    if (!errorStack) {
+        return;
+    }
 
     vfprintf(fd, format, va);
