Index: trunk/pswarp/src/pswarpLoopBackground.c
===================================================================
--- trunk/pswarp/src/pswarpLoopBackground.c	(revision 40354)
+++ trunk/pswarp/src/pswarpLoopBackground.c	(revision 41510)
@@ -55,4 +55,5 @@
     if (!status) {
         psError(PSWARP_ERR_DATA, true, "number of inputs is not defined (programming error)");
+	psFree (view);
         return false;
     }
@@ -90,15 +91,17 @@
 	pmFPAviewReset (view);
 
-	pmFPAview *view = pmFPAviewAlloc(0);
-	if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	pmFPAview *viewT2 = pmFPAviewAlloc(0);
+	if (!pmFPAfileIOChecks(config, viewT2, PM_FPA_BEFORE)) {
 	    psError(psErrorCodeLast(), false, "Unable to read files.");
+	    psFree (viewT2);
 	    goto FAIL;
 	}
 
 	pmChip *chip;
-	while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
+	while ((chip = pmFPAviewNextChip (viewT2, input->fpa, 1)) != NULL) {
 	    if (!chip->process || !chip->file_exists) { continue; }
-	    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+	    if (!pmFPAfileIOChecks(config, viewT2, PM_FPA_BEFORE)) {
 		psError(psErrorCodeLast(), false, "Unable to read files.");
+		psFree (viewT2);
 		goto FAIL;
 	    }
@@ -107,18 +110,20 @@
 	    double xBin = NAN;
 	    double yBin = NAN;
-	    pswarpGetInputScales (&xBin, &yBin, config, view, chip);
+	    pswarpGetInputScales (&xBin, &yBin, config, viewT2, chip);
 	    double fluxRatio = xBin * yBin / (float) (xGrid * yGrid);
 	    
-	    if (!pswarpModifyChipAstrom (config, view, chip, astrom, bilevelAstrometry, xBin, yBin)) {
+	    if (!pswarpModifyChipAstrom (config, viewT2, chip, astrom, bilevelAstrometry, xBin, yBin)) {
 		psError(psErrorCodeLast(), false, "failed to set BKGMODEL astrometry.");
+		psFree (viewT2);
 		goto FAIL;
 	    }
 
 	    pmCell *cell;
-	    while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
-		psTrace ("pswarp", 4, "ACell %d: %x %x %d\n", view->cell, cell->file_exists, cell->process,psErrorCodeLast());
+	    while ((cell = pmFPAviewNextCell (viewT2, input->fpa, 1)) != NULL) {
+		psTrace ("pswarp", 4, "ACell %d: %x %x %d\n", viewT2->cell, cell->file_exists, cell->process,psErrorCodeLast());
 		if (!cell->process || !cell->file_exists) { continue; }
-		if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
+		if (!pmFPAfileIOChecks (config, viewT2, PM_FPA_BEFORE)) {
 		    psError(psErrorCodeLast(), false, "Unable to read files.");
+		    psFree (viewT2);
 		    goto FAIL;
 		}
@@ -126,7 +131,8 @@
 		// process each of the readouts
 		pmReadout *readout;
-		while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
-		    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
+		while ((readout = pmFPAviewNextReadout(viewT2, input->fpa, 1)) != NULL) {
+		    if (!pmFPAfileIOChecks(config, viewT2, PM_FPA_BEFORE)) {
 			psError(psErrorCodeLast(), false, "Unable to read files.");
+			psFree (viewT2);
 			goto FAIL;
 		    }
@@ -136,15 +142,15 @@
 
 		    if (astrom != input) {
-		      pmReadout *astromRO = pmFPAviewThisReadout(view, astrom->fpa); // Readout for astrometry
-		      if ((!refcat)&&(astromRO)) {
-			if ((astromRO->parent->parent->hdu->header)&&(output->fpa->analysis)) {
-			  psMetadataItem *refItem = psMetadataLookup(astromRO->parent->parent->hdu->header, "PSREFCAT");
-			  if (refItem) {
-			    refcat = psMetadataLookupStr(NULL, astromRO->parent->parent->hdu->header, "PSREFCAT");
-			    psMetadataAddStr(output->fpa->analysis, PS_LIST_TAIL, "REFERENCE_CATALOG", PS_META_REPLACE,
-					     "Reference catalog used for calibration.", refcat);
-			  }
+			pmReadout *astromRO = pmFPAviewThisReadout(viewT2, astrom->fpa); // Readout for astrometry
+			if ((!refcat)&&(astromRO)) {
+			    if ((astromRO->parent->parent->hdu->header)&&(output->fpa->analysis)) {
+				psMetadataItem *refItem = psMetadataLookup(astromRO->parent->parent->hdu->header, "PSREFCAT");
+				if (refItem) {
+				    refcat = psMetadataLookupStr(NULL, astromRO->parent->parent->hdu->header, "PSREFCAT");
+				    psMetadataAddStr(output->fpa->analysis, PS_LIST_TAIL, "REFERENCE_CATALOG", PS_META_REPLACE,
+						     "Reference catalog used for calibration.", refcat);
+				}
+			    }
 			}
-		      }
 		    }
 		    
@@ -159,24 +165,28 @@
 		    pswarpTransformToTarget (output->fpa, readout, config, true);
 
-		    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { // Readout
+		    if (!pmFPAfileIOChecks (config, viewT2, PM_FPA_AFTER)) { // Readout
 			psError(psErrorCodeLast(), false, "Unable to close files.");
+			psFree (viewT2);
 			goto FAIL;
 		    }
 		}
 
-		if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { // Cell
+		if (!pmFPAfileIOChecks (config, viewT2, PM_FPA_AFTER)) { // Cell
 		    psError(psErrorCodeLast(), false, "Unable to close files.");
+		    psFree (viewT2);
 		    goto FAIL;
 		}
 	    }
 
-	    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { // Chip
+	    if (!pmFPAfileIOChecks (config, viewT2, PM_FPA_AFTER)) { // Chip
 		psError(psErrorCodeLast(), false, "Unable to close files.");
+		psFree (viewT2);
 		goto FAIL;
 	    }
 	}
 
-	if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) { // FPA
+	if (!pmFPAfileIOChecks (config, viewT2, PM_FPA_AFTER)) { // FPA
 	    psError(psErrorCodeLast(), false, "Unable to close files.");
+	    psFree (viewT2);
 	    goto FAIL;
 	}
@@ -184,6 +194,8 @@
 	if (!pswarpUpdateMetadata (output->fpa, skycell->fpa, input->fpa, astrom->fpa, config, false)) {
 	    psError(psErrorCodeLast(), false, "problem generating statistics.");
+	    psFree (viewT2);
 	    goto FAIL;
 	}
+	psFree (viewT2);
     }
 
