Index: trunk/psLib/src/imageops/psImageStructManip.c
===================================================================
--- trunk/psLib/src/imageops/psImageStructManip.c	(revision 14523)
+++ trunk/psLib/src/imageops/psImageStructManip.c	(revision 14921)
@@ -8,6 +8,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-08-16 02:51:09 $
+ *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-09-20 23:48:47 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -23,7 +23,8 @@
 
 #include "psMemory.h"
+#include "psError.h"
+#include "psAbort.h"
+
 #include "psImageStructManip.h"
-#include "psError.h"
-
 
 // col0,row0 are the starting pixel in the input image coordinate frame
@@ -98,10 +99,17 @@
     if (out != NULL) {
         // if a child, need to orphan (disassociate from parent) first
-        if (out->parent != NULL) {
-            psArrayRemoveData(out->parent->children,psMemIncrRefCounter(out));
-            // remove from parent's knowledge without triggering a free
-            out->parent = NULL; // break link to parent
+	psImage *parent = (psImage *) out->parent;
+        if (parent != NULL) {
+	    // break the back-pointer first so we don't loop
+	    out->parent = NULL;
+
+	    // drop my entry on my parent's array of children
+            psArrayRemoveDataNoFree (out->parent->children, out);
+
+	    // drop my reference to my old parent
+	    psFree (parent);
         }
 
+	// we recycle out->data.V
         psFree(out->p_rawDataBuffer); // free the previous data reference
     } else {
@@ -117,5 +125,5 @@
     P_PSIMAGE_SET_ROW0(out, row0);
 
-    out->parent = image;
+    out->parent = psMemIncrRefCounter(image); // track references to parents
     out->children = NULL;
     out->p_rawDataBuffer = rawData;
@@ -133,6 +141,6 @@
 
     // add output image as a child of the input image.
-    image->children = psArrayAdd(image->children,16,out);
-    psMemDecrRefCounter(out); // don't count the reference held by parent as a true reference
+    image->children = psArrayAdd (image->children, 16, out);
+    psFree (out); // the image->children array is an array of views only
 
     return (out);
@@ -307,4 +315,8 @@
                 _("Can not operate on a NULL psImage."));
         return NULL;
+    }
+
+    if ((image->children != NULL) && (image->children->n > 0)) {
+	psAbort ("cannot trim an image with outstanding children");
     }
 
@@ -345,6 +357,4 @@
     }
 
-    psImageFreeChildren(image);
-
     psU32 elementSize = PSELEMTYPE_SIZEOF(image->type.type);
     psU32 numCols = col1-col0;
