Index: trunk/psLib/src/imageops/psImageStructManip.c
===================================================================
--- trunk/psLib/src/imageops/psImageStructManip.c	(revision 21337)
+++ trunk/psLib/src/imageops/psImageStructManip.c	(revision 21347)
@@ -8,6 +8,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-11-08 01:09:48 $
+ *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-06 01:05:58 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -21,8 +21,10 @@
 #include <string.h>
 #include <assert.h>
+#include <errno.h>
 
 #include "psMemory.h"
 #include "psError.h"
 #include "psAbort.h"
+#include "psTrace.h"
 
 #include "psImageStructManip.h"
@@ -108,5 +110,7 @@
 
             // drop my entry on my parent's array of children
+	    psMutexLock (out);
             psArrayRemoveDataNoFree (out->parent->children, out);
+	    psMutexUnlock (out);
 
             // drop my reference to my old parent
@@ -119,4 +123,5 @@
         out = p_psAlloc(file, lineno, func, sizeof(psImage));
         out->data.V = NULL;
+	psMutexInit (out);
     }
 
@@ -129,4 +134,6 @@
     P_PSIMAGE_SET_ROW0(out, row0);
 
+    // As long as I have a valid image reference, no one else can free it to zero (I have at
+    // least the last reference)
     out->parent = psMemIncrRefCounter(image); // track references to parents
     out->children = NULL;
@@ -144,8 +151,11 @@
     }
 
-    // add output image as a child of the input image.
+    // Add output image as a child of the input image.  Lock image before performing this
+    // operation (psArrayAdd is NOT thread-safe)
+    psMutexLock (image);
     image->children = p_psArrayAdd(file, lineno, func, image->children, 16, out);
+    psMutexUnlock (image);
+
     psFree (out); // the image->children array is an array of views only
-
     return (out);
 }
@@ -369,5 +379,5 @@
     P_PSIMAGE_SET_NUMROWS(image, numRows);
 
-    // XXX: should I really resize the buffers?
+    // resize the buffers to the new image size
     image->data.V = psRealloc(image->data.V,sizeof(psPtr)*numRows);
     image->p_rawDataBuffer = psRealloc(image->p_rawDataBuffer,rowSize*numRows);
