Index: trunk/psphot/src/psphotSourceFits.c
===================================================================
--- trunk/psphot/src/psphotSourceFits.c	(revision 6056)
+++ trunk/psphot/src/psphotSourceFits.c	(revision 6379)
@@ -54,5 +54,5 @@
 
     // extended source model descriptions
-    char *modelNameEXT = psMetadataLookupPtr (&status, config, "EXT_MODEL");
+    char *modelNameEXT = psMetadataLookupStr (&status, config, "EXT_MODEL");
     modelTypeEXT = pmModelSetType (modelNameEXT);
     psphotInitRadiusEXT (config, sky, modelTypeEXT);
@@ -77,4 +77,5 @@
     psTrace ("psphot.blend", 5, "trying blob...\n");
 
+    // this temporary source is used as a place-holder by the psphotEval functions below
     pmSource *tmpSrc = pmSourceAlloc ();
 
@@ -89,4 +90,6 @@
     chiDBL = ONE->chisq / ONE->nDOF;
 
+    psFree (tmpSrc); // XXX should I keep / save the flags set in the eval functions?
+
     if (okEXT && okDBL) {
 	psTrace ("psphot.blend", 5, "blob chisq: %f vs %f\n", chiEXT, chiDBL);
@@ -98,8 +101,13 @@
     if (okEXT && !okDBL) goto keepEXT;
     if (!okEXT && okDBL) goto keepDBL;
+
+    // both models failed; reject them both
+    psFree (EXT);
+    psFree (DBL);
     return false;
 
 keepEXT:
     // sub EXT
+    psFree (DBL);
     pmSourceSubModel (source->pixels, source->mask, EXT, false, false);
     psTrace ("psphot.blend", 5, "blob as EXT: %f %f\n", EXT->params->data.F32[2], EXT->params->data.F32[3]);
@@ -113,4 +121,5 @@
 keepDBL:
     // sub DLB
+    psFree (EXT);
     pmSourceSubModel (source->pixels, source->mask, (pmModel *) DBL->data[0], false, false);
     pmSourceSubModel (source->pixels, source->mask, (pmModel *) DBL->data[1], false, false);
@@ -120,8 +129,4 @@
     source->mode |=  PM_SOURCE_SUBTRACTED;
     source->mode &= ~PM_SOURCE_TEMPSUB;
-
-    // save new model
-    // tmpSrc->modelPSF = (pmModel *) modelSet->data[1];
-    // psArrayAdd (sources, 100, tmpSrc);
 
     return true;
@@ -205,5 +210,4 @@
 	return status;
     }
-
     psTrace ("psphot.blend", 5, "trying blend...\n");
 
@@ -225,4 +229,8 @@
     psArrayAdd (sourceSet, 16, source);
 
+    // counter to track the blend elements used in the fit
+    psVector *index  = psVectorAlloc (source->blends->n + 1, PS_TYPE_S32);
+    index->data.S32[0] = -1; // first element corresponds to the primary source
+
     for (int i = 0; i < source->blends->n; i++) {
 	pmSource *blend = source->blends->data[i];
@@ -237,7 +245,7 @@
 	pmModel *model = blend->modelPSF;
 
-	for (psS32 i = 0; i < model->params->n; i++) {
-	    model->params->data.F32[i] = PSF->params->data.F32[i];
-	    model->dparams->data.F32[i] = PSF->dparams->data.F32[i];
+	for (int j = 0; j < model->params->n; j++) {
+	    model->params->data.F32[j] = PSF->params->data.F32[j];
+	    model->dparams->data.F32[j] = PSF->dparams->data.F32[j];
 	}
 
@@ -247,4 +255,5 @@
 
 	// add this blend to the list
+	index->data.S32[modelSet->n] = i;
 	psArrayAdd (modelSet, 16, model);
 	psArrayAdd (sourceSet, 16, blend);
@@ -261,7 +270,10 @@
 	pmModel *model = modelSet->data[i];
 
+	// if we skip this one, free the corresponding blend entry model
 	if (!psphotEvalPSF (src, model)) {
-	    psFree (model);
-	    src->modelPSF = NULL;
+	    int n = index->data.S32[i];
+	    pmSource *blend = source->blends->data[n];
+	    psFree (blend->modelPSF);
+	    blend->modelPSF = NULL;
 	    continue;
 	}
@@ -272,4 +284,7 @@
 	src->mode &= ~PM_SOURCE_TEMPSUB;
     }
+    psFree (index);
+    psFree (modelSet);
+    psFree (sourceSet);
 
     // evaluate the primary object
@@ -281,4 +296,5 @@
     psTrace ("psphot.blend", 5, "fitted primary as PSF\n");
     pmSourceSubModel (source->pixels, source->mask, PSF, false, false);
+    psFree (source->modelPSF);
     source->modelPSF = PSF;
     source->mode |=  PM_SOURCE_SUBTRACTED;
