Index: trunk/psphot/src/psphotBasicDeblend.c
===================================================================
--- trunk/psphot/src/psphotBasicDeblend.c	(revision 6319)
+++ trunk/psphot/src/psphotBasicDeblend.c	(revision 6379)
@@ -1,6 +1,5 @@
 # include "psphot.h"
 
-// 2006.02.02 : no leaks? (creates source->blend entries)
-// XXX if I free sources, I still have 54 leaks?
+// 2006.02.07 : no leaks
 bool psphotBasicDeblend (psArray *sources, psMetadata *config, psStats *sky) { 
 
@@ -35,7 +34,4 @@
     // this results in an index of increasing SN
 
-    // temporary array for overlapping objects we find
-    psArray *overlap = psArrayAlloc (100);
-
     // examine sources in decreasing SN order
     for (int i = sources->n - 1; i >= 0; i--) {
@@ -45,4 +41,10 @@
 	if (source->mode & PM_SOURCE_BLEND) continue;
 
+	// temporary array for overlapping objects we find
+	// XXX psArrayAlloc should set ->n to 0 *and* all objects to NULL
+	// XXX I need a psArrayEmpty function to free the elements without the array
+	// XXX then, I could allocate 'overlap' once outside the loop and only
+	// XXX clear at the end of each loop
+	psArray *overlap = psArrayAlloc (100);
 	overlap->n = 0;
 
@@ -73,54 +75,61 @@
 	}
 
+	if (overlap->n == 0) {
+	    psFree (overlap);
+	    continue;
+	}
+
+	// this source has overlapping neighbors, check for actual blends
 	// generate source contour (1/4 peak counts)
-	if (overlap->n > 0) {
-	    // set the threshold based on user inputs
-	    threshold = FRACTION * (source->peak->counts - source->moments->Sky) + source->moments->Sky; 
-	    threshold = PS_MAX (threshold, minThreshold);
+	// set the threshold based on user inputs
+	threshold = FRACTION * (source->peak->counts - source->moments->Sky) + source->moments->Sky; 
+	threshold = PS_MAX (threshold, minThreshold);
 
-	    // generate a basic contour (set of x,y coordinates at-or-below flux level)
-	    psArray *contour = pmSourceContour_EAM (source->pixels, source->peak->x, source->peak->y, threshold);
-	    if (contour == NULL) continue;
+	// generate a basic contour (set of x,y coordinates at-or-below flux level)
+	psArray *contour = pmSourceContour_EAM (source->pixels, source->peak->x, source->peak->y, threshold);
+	if (contour == NULL) {
+	    psFree (overlap);
+	    continue;
+	}
 
-	    // the source contour consists of two vectors, xv and yv.  the contour is 
-	    // a series of coordinate pairs, (xv[i],yv[i]) & (xv[i+1],yv[i+1]).  both
-	    // coordinate pairs have the same yv[] value, with xv[i] corresponding to
-	    // the left boundary and xv[i+1] corresponding to the right boundary
+	// the source contour consists of two vectors, xv and yv.  the contour is 
+	// a series of coordinate pairs, (xv[i],yv[i]) & (xv[i+1],yv[i+1]).  both
+	// coordinate pairs have the same yv[] value, with xv[i] corresponding to
+	// the left boundary and xv[i+1] corresponding to the right boundary
 
-	    psVector *xv = contour->data[0];
-	    psVector *yv = contour->data[1];
-	    for (int k = 0; k < overlap->n; k++) {
-		testSource = overlap->data[k];
-		if (testSource->peak->counts > source->peak->counts) continue;
-		for (int j = 0; j < xv->n; j+=2) {
-		    if (fabs(yv->data.F32[j] - testSource->peak->y) > 0.5) continue;
-		    if (xv->data.F32[j+0] > testSource->peak->x) break;
-		    if (xv->data.F32[j+1] < testSource->peak->x) break;
+	psVector *xv = contour->data[0];
+	psVector *yv = contour->data[1];
+	for (int k = 0; k < overlap->n; k++) {
+	    testSource = overlap->data[k];
+	    if (testSource->peak->counts > source->peak->counts) continue;
+	    for (int j = 0; j < xv->n; j+=2) {
+		if (fabs(yv->data.F32[j] - testSource->peak->y) > 0.5) continue;
+		if (xv->data.F32[j+0] > testSource->peak->x) break;
+		if (xv->data.F32[j+1] < testSource->peak->x) break;
 
-		    testSource->mode |= PM_SOURCE_BLEND;
+		testSource->mode |= PM_SOURCE_BLEND;
 
-		    // add this to the list of source->blends
-		    if (source->blends == NULL) {
-			source->blends = psArrayAlloc (16);
-			source->blends->n = 0;
-		    }
-		    psArrayAdd (source->blends, 16, testSource);
+		// add this to the list of source->blends
+		if (source->blends == NULL) {
+		    source->blends = psArrayAlloc (16);
+		    source->blends->n = 0;
+		}
 
-		    Nblend ++;
-		    j = xv->n;
-		}
-	    }  
-	    psFree (contour);
-	}
+		psArrayAdd (source->blends, 16, testSource);
+
+		Nblend ++;
+		j = xv->n;
+	    }
+	}  
+	psFree (overlap);
+	psFree (contour);
     }
     psLogMsg ("psphot.deblend", 3, "identified %d blended objects (%f sec)\n", Nblend, psTimerMark ("psphot"));
 
-    char *breakPt = psMetadataLookupPtr (&status, config, "BREAK_POINT");
+    char *breakPt = psMetadataLookupStr (&status, config, "BREAK_POINT");
     if (!strcasecmp (breakPt, "DEBLEND")) exit (0);
 
     psFree (SN);
     psFree (index);
-    psFree (overlap);
-    
     return true;
 }
