Index: /trunk/psphot/src/LocalSky.c
===================================================================
--- /trunk/psphot/src/LocalSky.c	(revision 4641)
+++ /trunk/psphot/src/LocalSky.c	(revision 4642)
@@ -25,4 +25,5 @@
     mySource->mask = subImageMask;
     mySource->noise = subImageNoise;
+    psFree (srcRegion);
 
     return(mySource);
@@ -47,4 +48,5 @@
     myStats = psImageStats(myStats, image, mask, 0xff);
     psImageMaskRegion (mask, srcRegion, AND, 0x7f);
+    psFree (srcRegion);
 
     psF64 tmpF64;
@@ -54,5 +56,4 @@
     source->moments = pmMomentsAlloc();
     source->moments->Sky = (psF32) tmpF64;
-
     return (true);
 }
Index: /trunk/psphot/src/apply_psf_model.c
===================================================================
--- /trunk/psphot/src/apply_psf_model.c	(revision 4641)
+++ /trunk/psphot/src/apply_psf_model.c	(revision 4642)
@@ -39,5 +39,8 @@
 	if (source->type == PS_SOURCE_SATURATED) continue;
 
-	source->modelPSF = NULL;
+	// do I even need to test?
+	if (source->modelPSF != NULL) {
+	  psFree (source->modelPSF);
+	}
 
 	// use the source moments, etc to guess basic model parameters
Index: /trunk/psphot/src/basic_classes.c
===================================================================
--- /trunk/psphot/src/basic_classes.c	(revision 4641)
+++ /trunk/psphot/src/basic_classes.c	(revision 4642)
@@ -8,7 +8,4 @@
     psfClump = pmSourcePSFClump (sources, config);
     pmSourceRoughClass (sources, config, psfClump);
-
-    // make this optional
-    // DumpMoments (sources, "moments.dat");
     return (true);
 }
Index: /trunk/psphot/src/choose_psf_model.c
===================================================================
--- /trunk/psphot/src/choose_psf_model.c	(revision 4641)
+++ /trunk/psphot/src/choose_psf_model.c	(revision 4642)
@@ -34,8 +34,11 @@
 	modelName = psMetadataLookupPtr (&status, config, key);
 	tests->data[i] = pmPSF_TestModel (stars, modelName, RADIUS);
+	psFree (modelName);
 
-	sprintf (filename, "metric.%d.dat", i);
-	DumpPSFTestData (tests->data[i], filename);
+	// make this an option:
+	// sprintf (filename, "metric.%d.dat", i);
+	// DumpPSFTestData (tests->data[i], filename);
     }
+    psFree (stars);
 
     // select the best of the test models
@@ -46,5 +49,5 @@
     for (int i = 1; i < Ntest; i++) {
 	test = tests->data[i];
-	int M = test->dApResid;
+	float M = test->dApResid;
 	if (M < bestM) {
 	    bestM = M;
@@ -55,5 +58,8 @@
     modelName = psModelGetType (test->modelType);
     psLogMsg ("psphot.pspsf", 3, "selected psf model %s, ApResid: %f +/- %f\n", modelName, test->ApResid, test->dApResid);
+    psFree (modelName);
 
+    // we are keeping the modelPSF fits from the PSF test, 
+    // but we replace these later on (apply_psf_model)
     // set source->model based on best psf model fit
     for (int i = 0; i < test->sources->n; i++) {
@@ -67,4 +73,10 @@
 	}
     }
-    return (test->psf);
+    pmPSF *psf = test->psf;
+
+    // free unused test models:
+    psFree (test);
+    psFree (stars);
+
+    return (psf);
 }
Index: /trunk/psphot/src/find_peaks.c
===================================================================
--- /trunk/psphot/src/find_peaks.c	(revision 4641)
+++ /trunk/psphot/src/find_peaks.c	(revision 4642)
@@ -9,5 +9,4 @@
 
     // smooth the image 
-    // should we also subtract a super-binned image? (as an option?)
 
     psTimerStart ("psphot");
@@ -18,11 +17,9 @@
     psImage *smooth = psImageCopy (NULL, imdata->image, PS_TYPE_F32);
     psImageSmooth (smooth, SIGMA, NSIGMA);
-
     psLogMsg ("psphot", 4, "smooth: %f sec\n", psTimerMark ("psphot"));
-
-    // find the peaks in the smoothed image 
 
     psTimerStart ("psphot");
 
+    // find the peaks in the smoothed image 
     NSIGMA    = psMetadataLookupF32 (&status, config, "PEAK_NSIGMA");
     threshold = NSIGMA*sky->sampleStdev + sky->sampleMean;
@@ -32,5 +29,4 @@
     if (peaks == NULL) psAbort ("find peaks", "no peaks found");
     psFree (smooth);
-    // XXX Need to gracefully handle no source detections
 
     // make this optional
@@ -41,2 +37,6 @@
     return (peaks);
 }
+
+// In this function, we smooth the image, then search for the peaks 
+// Should we also subtract a super-binned image? (as an option?)
+// We need to gracefully handle no source detections
Index: /trunk/psphot/src/fit_galaxies.c
===================================================================
--- /trunk/psphot/src/fit_galaxies.c	(revision 4641)
+++ /trunk/psphot/src/fit_galaxies.c	(revision 4642)
@@ -32,8 +32,10 @@
 	if (source->type == PS_SOURCE_SATSTAR) continue;
 	if (source->type == PS_SOURCE_GOODSTAR) continue;
+
 	// skip all likely defects
 	if (source->type == PS_SOURCE_DEFECT) continue;
 	if (source->type == PS_SOURCE_SATURATED) continue;
-	// 
+
+	// skip poorly fitted stars
 	if (source->type == PS_SOURCE_FAINTSTAR) continue;
 	if (source->type == PS_SOURCE_POOR_FIT_PSF) continue;
Index: /trunk/psphot/src/image_stats.c
===================================================================
--- /trunk/psphot/src/image_stats.c	(revision 4641)
+++ /trunk/psphot/src/image_stats.c	(revision 4642)
@@ -35,4 +35,6 @@
 	stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN); 
 	stats = psVectorStats (stats, subset, NULL, NULL, 0); // use a mask?
+	psFree (subset);
+	psFree (rnd)
     }
 
Index: /trunk/psphot/src/pspsf.c
===================================================================
--- /trunk/psphot/src/pspsf.c	(revision 4641)
+++ /trunk/psphot/src/pspsf.c	(revision 4642)
@@ -21,4 +21,5 @@
 	psf->params->data[i] = Polynomial2DAlloc(1, 1, PS_POLYNOMIAL_ORD);
     }
+    // set the destructors
     return(psf);
 }
@@ -48,4 +49,6 @@
 	test->fitMag->data.F64[i] = 0;
     }	
+    // set the destructors
+
     return (test);
 }
@@ -92,5 +95,7 @@
     psLogMsg ("psphot.psftest", 4, "fit flt:   %f sec for %d sources\n", psTimerMark ("fit"), sources->n);
     psTrace ("psphot.psftest", 3, "keeping %d of %d PSF candidates (FLT)\n", Nflt, sources->n);
-    DumpModelFits (test->modelFLT, "modelsFLT.dat");
+
+    // make this optional? 
+    // DumpModelFits (test->modelFLT, "modelsFLT.dat");
 
     // stage 2: construct a psf (pmPSF) from this collection of model fits
@@ -141,5 +146,7 @@
     psLogMsg ("psphot.psftest", 4, "fit psf:   %f sec for %d sources\n", psTimerMark ("fit"), sources->n);
     psTrace ("psphot.psftest", 3, "keeping %d of %d PSF candidates (PSF)\n", Npsf, sources->n);
-    DumpModelFits (test->modelPSF, "modelsPSF.dat");
+
+    // make this optional
+    // DumpModelFits (test->modelPSF, "modelsPSF.dat");
 
     // XXX this function wants aperture radius from pmSourcePhotometry
@@ -188,4 +195,9 @@
 	// psPolynomial2DDump (psf->params->data[i]);
     }
+    
+    psFree (x);
+    psFree (y);
+    psFree (z);
+    psFree (dz);
     return (true);
 }
@@ -228,9 +240,4 @@
     rflux->data.F64[i] = pow(10.0, 0.4*test->fitMag->data.F64[i]);
   }
-  // psScalar *t1 = psScalar(0.4);
-  // psVector *v1 = psBinaryOp (NULL, test->fitMag, "*", t1);
-  // psVector *rflux = psUnaryOp (NULL, v1, "ten");
-  // psFree (t1);
-  // psFree (v1);
 
   // find min and max of (1/flux):
@@ -247,4 +254,5 @@
   psTrace ("psphot.metricmodel", 3, "rflux max: %g, min: %g, delta: %g\n", stats->max, stats->min, dBin);
 
+  // group data in daBin bins, measure lower 50% mean
   for (int i = 0; i < daBin->n; i++) {
 
@@ -277,4 +285,12 @@
     // dA values are contaminated with low outliers 
     // measure statistics only on upper 50% of points
+    // this would be easier if we could sort in reverse:
+    //
+    // psVectorSort (tmp, tmp);
+    // tmp->n = 0.5*tmp->n;
+    // stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN);
+    // psVectorStats (stats, tmp, NULL, NULL, 0);
+    // psTrace ("psphot.metricmodel", 4, "rfBin %d (%g): %d pts, %g\n", i, rfBin->data.F64[i], tmp->n, stats->sampleMedian);
+
     psVectorSort (tmp, tmp);
     nKeep = 0.5*tmp->n;
@@ -291,4 +307,8 @@
 
     daBin->data.F64[i] = stats->sampleMedian;
+
+    psFree (stats);
+    psFree (tmp);
+    psFree (tmp2);
   }
 
@@ -307,4 +327,12 @@
   test->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
 
+  psFree (maskB);
+  psFree (rfBin);
+  psFree (daBin);
+  psFree (daBinFit);
+  psFree (daResid);
+  psFree (poly);
+  psFree (stats);
+
   return true;
 }
Index: /trunk/psphot/src/setup.c
===================================================================
--- /trunk/psphot/src/setup.c	(revision 4641)
+++ /trunk/psphot/src/setup.c	(revision 4642)
@@ -83,6 +83,7 @@
     float YMAX  = psMetadataLookupF32 (&status, config, "YMAX");
     psRegion *keep = psRegionAlloc (XMIN, XMAX, YMIN, YMAX);
-    keep           = psRegionForImage (keep, image, keep);
+    keep = psRegionForImage (keep, image, keep);
     psImageKeepRegion (mask, keep, OR, 0x01);
+    psFree (keep);
 
     // mask the saturated pixels
