Index: /branches/eam_branch_20090203/psLib/src/imageops/psImageStructManip.c
===================================================================
--- /branches/eam_branch_20090203/psLib/src/imageops/psImageStructManip.c	(revision 21293)
+++ /branches/eam_branch_20090203/psLib/src/imageops/psImageStructManip.c	(revision 21294)
@@ -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.20.42.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-04 23:36:56 $
  *
  *  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"
@@ -100,4 +102,5 @@
     psPtr rawData = psMemIncrRefCounter(image->p_rawDataBuffer);
 
+    // XXX worry about this later...
     if (out != NULL) {
         // if a child, need to orphan (disassociate from parent) first
@@ -108,5 +111,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 +124,5 @@
         out = p_psAlloc(file, lineno, func, sizeof(psImage));
         out->data.V = NULL;
+	psMutexInit (out);
     }
 
@@ -129,4 +135,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 +152,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);
 }
Index: /branches/eam_branch_20090203/psLib/src/mathtypes/psImage.c
===================================================================
--- /branches/eam_branch_20090203/psLib/src/mathtypes/psImage.c	(revision 21293)
+++ /branches/eam_branch_20090203/psLib/src/mathtypes/psImage.c	(revision 21294)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.132 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-11-05 11:12:39 $
+ *  @version $Revision: 1.132.10.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-04 23:36:56 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -24,4 +24,5 @@
 #include <math.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "psMemory.h"
@@ -29,4 +30,5 @@
 #include "psAssert.h"
 #include "psAbort.h"
+#include "psTrace.h"
 
 #include "psImage.h"
@@ -53,5 +55,8 @@
 
         // drop my entry on my parent's array of children
+	// lock parent before freeing child : psArrayRemoveDataNoFree is NOT thread safe
+	psMutexLock (parent);
         psArrayRemoveDataNoFree (parent->children, image);
+	psMutexUnlock (parent);
 
         // drop my reference to my parent
@@ -66,4 +71,5 @@
     }
 
+    psMutexDestroy(image);
     psFree(image->children);
     psFree(image->p_rawDataBuffer);
@@ -122,4 +128,7 @@
     image->parent = NULL;
     image->children = NULL;
+
+    // XXX for a test : we will add a mutex to all images:
+    psMutexInit (image);
 
     return image;
