Index: trunk/psphot/src/pspsf.c
===================================================================
--- trunk/psphot/src/pspsf.c	(revision 4642)
+++ trunk/psphot/src/pspsf.c	(revision 4901)
@@ -1,3 +1,11 @@
 # include "psphot.h"
+
+static void pmPSFFree (pmPSF *psf) {
+
+  if (psf == NULL) return;
+
+  psFree (psf->params);
+  return;
+}
 
 // a PSF always has 4 parameters fewer than the equivalent model
@@ -16,4 +24,5 @@
 	return(NULL);
     }      
+
     psf->params = psArrayAlloc (Nparams - 4);
     for (int i = 0; i < psf->params->n; i++) {
@@ -21,6 +30,21 @@
 	psf->params->data[i] = Polynomial2DAlloc(1, 1, PS_POLYNOMIAL_ORD);
     }
-    // set the destructors
+
+    p_psMemSetDeallocator(psf, (psFreeFcn) pmPSFFree);
     return(psf);
+}
+
+static void pmPSF_TestFree (pmPSF_Test *test) {
+
+  if (test == NULL) return;
+
+  psFree (test->psf);
+  psFree (test->sources);
+  psFree (test->modelFLT);
+  psFree (test->modelPSF);
+  psFree (test->metric);
+  psFree (test->fitMag);
+  psFree (test->mask);
+  return;
 }
 
@@ -32,5 +56,5 @@
     test->modelType   = psModelSetType (modelName);
     test->psf         = pmPSFAlloc (test->modelType);
-    test->sources     = sources;
+    test->sources     = psMemCopy(sources);
     test->modelFLT    = psArrayAlloc (sources->n);
     test->modelPSF    = psArrayAlloc (sources->n);
@@ -49,6 +73,6 @@
 	test->fitMag->data.F64[i] = 0;
     }	
-    // set the destructors
-
+
+    p_psMemSetDeallocator(test, (psFreeFcn) pmPSF_TestFree);
     return (test);
 }
@@ -74,4 +98,5 @@
     psTimerStart ("fit");
     for (int i = 0; i < test->sources->n; i++) {
+
 	psSource *source = test->sources->data[i];
 	psModel  *model  = pmSourceModelGuess (source, test->modelType); 
@@ -88,4 +113,5 @@
 	if (!status) {
 	  test->mask->data.U8[i] = 2;
+	  psFree (model);
 	  continue;
 	}
@@ -122,4 +148,5 @@
 	if (!status) {
 	    test->mask->data.U8[i] = 3;
+	    psFree (modelPSF);
 	    goto next_source;
 	}
@@ -154,4 +181,5 @@
     psLogMsg ("psphot.pspsf", 3, "test model %s, ap-fit: %f +/- %f, sky bias: %f\n", 
 	      modelName, test->ApResid, test->dApResid, test->skyBias);
+
     return (test);
 }
@@ -223,5 +251,4 @@
   float dBin;
   int   nKeep, nSkip;
-  
 
   // the measured (aperture - fit) magnitudes (dA == test->metric)
@@ -280,4 +307,5 @@
     if (tmp->n < 2) {
       maskB->data.U8[i] = 1;
+      psFree (tmp);
       continue;
     } 
@@ -327,7 +355,8 @@
   test->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
 
-  psFree (maskB);
+  psFree (rflux);
   psFree (rfBin);
   psFree (daBin);
+  psFree (maskB);
   psFree (daBinFit);
   psFree (daResid);
