Index: /branches/eam_branch_20070830/psLib/src/mathtypes/psImage.c
===================================================================
--- /branches/eam_branch_20070830/psLib/src/mathtypes/psImage.c	(revision 14902)
+++ /branches/eam_branch_20070830/psLib/src/mathtypes/psImage.c	(revision 14903)
@@ -9,6 +9,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.129 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-04-12 18:52:57 $
+ *  @version $Revision: 1.129.8.1 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-20 19:14:16 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -28,4 +28,6 @@
 #include "psError.h"
 #include "psAssert.h"
+#include "psAbort.h"
+
 #include "psImage.h"
 #include "psString.h"
@@ -38,16 +40,31 @@
     }
 
-    if (image->parent != NULL) {
-        psMemBlock* ptr = ((psMemBlock*)image)-1;
-        int ref = ptr->refCounter;
-        ptr->refCounter = 2;  // make sure psFree is not retriggered
-        psArrayRemoveData(image->parent->children,image);
-        ptr->refCounter = ref; // restore previous count (not assuming zero, but should be)
-
-        image->parent = NULL;
-    }
-
-    psImageFreeChildren(image);
-
+    psImage *parent = (psImage *) image->parent;
+
+    // if I am a child, remove me from my parent's array of children
+    if (parent != NULL) {
+	// sanity check : a child cannot also be a parent
+	if ((image->children != NULL) && (image->children->n > 0)) {
+	    psAbort ("psImage cannot be both child and parent!");
+	}
+
+	// break the back-pointer first so we don't loop
+        image->parent = NULL; 
+
+	// drop my entry on my parent's array of children
+	psArrayRemoveDataNoFree (parent->children, image); 
+	
+	// drop my reference to my parent
+	psFree (parent);
+    }
+    
+    // sanity check: this function should never be reached if an image still has live children;
+    // they should each be holding a pointer to the image, forcing the number of references to
+    // be > 1.
+    if (image->children && (image->children->n > 0)) {
+	psAbort ("psImage memory management programming error : imageFree called on image with live children");
+    }
+
+    psFree(image->children);
     psFree(image->p_rawDataBuffer);
     psFree(image->data.V);
@@ -524,30 +541,4 @@
 }
 
-
-int psImageFreeChildren(psImage* image)
-{
-    psS32 numFreed = 0;
-
-    if (image == NULL) {
-        return numFreed;
-    }
-
-    if (image->children != NULL) {
-        psImage** children = (psImage**)image->children->data;
-        numFreed = image->children->n;
-
-        // orphan the children first
-        // (so psFree doesn't try to modify the parent's children array while I'm using it)
-        for (psS32 i=0;i<numFreed;i++) {
-            children[i]->parent = NULL;
-        }
-
-        psFree(image->children);
-        image->children = NULL;
-    }
-
-    return numFreed;
-}
-
 bool p_psImagePrint (int fd,
                      psImage *a,
