Index: trunk/psphot/src/choose_psf_model.c
===================================================================
--- trunk/psphot/src/choose_psf_model.c	(revision 4642)
+++ trunk/psphot/src/choose_psf_model.c	(revision 4901)
@@ -7,5 +7,5 @@
     bool        status;
     char       *modelName;
-    char        key[64], filename[64];
+    char        key[64];
     pmPSF_Test *test  = NULL;
     psArray    *stars = NULL;
@@ -31,6 +31,7 @@
     psArray *tests = psArrayAlloc (Ntest);
     for (int i = 0; i < tests->n; i++) { 
-	sprintf (key, "PSF_MODEL_%d", i);
+	snprintf (key, 64, "PSF_MODEL_%d", i);
 	modelName = psMetadataLookupPtr (&status, config, key);
+
 	tests->data[i] = pmPSF_TestModel (stars, modelName, RADIUS);
 	psFree (modelName);
@@ -55,9 +56,11 @@
 	}
     }
+    // keep only the selected test:
     test = tests->data[bestN];
     modelName = psModelGetType (test->modelType);
     psLogMsg ("psphot.pspsf", 3, "selected psf model %s, ApResid: %f +/- %f\n", modelName, test->ApResid, test->dApResid);
-    psFree (modelName);
 
+    // XXX I am having trouble tracking down the double frees vs leaks
+    # if (0)
     // we are keeping the modelPSF fits from the PSF test, 
     // but we replace these later on (apply_psf_model)
@@ -70,13 +73,12 @@
 	  source->modelPSF = NULL;
 	} else {
+	  // source->modelPSF = psMemCopy(test->modelPSF->data[i]);
 	  source->modelPSF = test->modelPSF->data[i];
 	}
     }
-    pmPSF *psf = test->psf;
+    # endif
 
-    // free unused test models:
-    psFree (test);
-    psFree (stars);
-
+    pmPSF *psf = psMemCopy(test->psf);
+    psFree (tests);
     return (psf);
 }
