From 849df8b6dd7de1d45eaf8af1f4f2b1c1d72e5c9c Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 11 Jul 2012 22:54:53 +0200 Subject: Fix a load of memory leaks --- libcrystfel/src/peaks.c | 32 ++++++++-- libcrystfel/src/reax.c | 7 +++ src/indexamajig.c | 154 ++++++++++++++++++++++++++++++++++-------------- 3 files changed, 142 insertions(+), 51 deletions(-) diff --git a/libcrystfel/src/peaks.c b/libcrystfel/src/peaks.c index ffe77290..047bac34 100644 --- a/libcrystfel/src/peaks.c +++ b/libcrystfel/src/peaks.c @@ -160,6 +160,8 @@ static int *make_BgMask(struct image *image, struct panel *p, mask = calloc(w*h, sizeof(int)); if ( mask == NULL ) return NULL; + if ( image->reflections == NULL ) return mask; + /* Loop over all reflections */ for ( refl = first_refl(image->reflections, &iter); refl != NULL; @@ -278,17 +280,26 @@ static int integrate_peak(struct image *image, int cfs, int css, /* It must have all the "good" bits to be valid */ if ( !((flags & image->det->mask_good) - == image->det->mask_good) ) return 1; + == image->det->mask_good) ) { + free(bgPkMask); + return 1; + } /* If it has any of the "bad" bits, reject */ - if ( flags & image->det->mask_bad ) return 1; + if ( flags & image->det->mask_bad ) { + free(bgPkMask); + return 1; + } } val = image->data[idx]; /* Veto peak if it contains saturation in bg region */ - if ( use_max_adu && (val > p->max_adu) ) return 1; + if ( use_max_adu && (val > p->max_adu) ) { + free(bgPkMask); + return 1; + } bg_tot += val; bg_tot_sq += pow(val, 2.0); @@ -330,17 +341,26 @@ static int integrate_peak(struct image *image, int cfs, int css, /* It must have all the "good" bits to be valid */ if ( !((flags & image->det->mask_good) - == image->det->mask_good) ) return 1; + == image->det->mask_good) ) { + free(bgPkMask); + return 1; + } /* If it has any of the "bad" bits, reject */ - if ( flags & image->det->mask_bad ) return 1; + if ( flags & image->det->mask_bad ) { + free(bgPkMask); + return 1; + } } val = image->data[idx] - bg_mean; /* Veto peak if it contains saturation */ - if ( use_max_adu && (image->data[idx] > p->max_adu) ) return 1; + if ( use_max_adu && (image->data[idx] > p->max_adu) ) { + free(bgPkMask); + return 1; + } pk_counts++; pk_total += val; diff --git a/libcrystfel/src/reax.c b/libcrystfel/src/reax.c index 58dce40a..3124a77c 100644 --- a/libcrystfel/src/reax.c +++ b/libcrystfel/src/reax.c @@ -348,6 +348,7 @@ static double iterate_refine_vector(double *x, double *y, double *z, gsl_matrix_free(C); gsl_vector_free(A); + gsl_vector_free(t); return dtmax; } @@ -1019,6 +1020,7 @@ void reax_index(IndexingPrivate *pp, struct image *image, UnitCell *cell) fftw_complex *fft_out; double pmax; struct reax_search *s; + int i; assert(pp->indm == INDEXING_REAX); p = (struct reax_private *)pp; @@ -1045,6 +1047,11 @@ void reax_index(IndexingPrivate *pp, struct image *image, UnitCell *cell) assemble_cells_from_candidates(image, s, cell); + for ( i=0; in_search; i++ ) { + free(s->search[i].cand); + } + free(s->search); + free(s); fftw_free(fft_in); fftw_free(fft_out); } diff --git a/src/indexamajig.c b/src/indexamajig.c index 1db8b353..33caddfa 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -99,14 +99,12 @@ struct index_args int cellr; float tols[4]; struct beam_params *beam; - const char *element; - const char *hdf5_peak_path; + char *element; + char *hdf5_peak_path; double ir_inn; double ir_mid; double ir_out; - - /* Output stream */ - const struct copy_hdf5_field *copyme; + struct copy_hdf5_field *copyme; }; @@ -366,11 +364,8 @@ static void process_image(const struct index_args *iargs, case PEAK_ZAEF: search_peaks(&image, iargs->threshold, - iargs->min_gradient, - iargs->min_snr, - iargs->ir_inn, - iargs->ir_mid, - iargs->ir_out); + iargs->min_gradient, iargs->min_snr, + iargs->ir_inn, iargs->ir_mid, iargs->ir_out); break; } @@ -483,9 +478,20 @@ static void run_work(const struct index_args *iargs, free(line); } + /* close my pipes */ fclose(fh); close(results_pipe); + + cleanup_indexing(iargs->ipriv); + free(iargs->indm); + free(iargs->ipriv); + free_detector_geometry(iargs->det); + free(iargs->beam); + free(iargs->element); + free(iargs->hdf5_peak_path); + free_copy_hdf5_field_list(iargs->copyme); + cell_free(iargs->cell); } @@ -616,10 +622,13 @@ static void run_reader(int *stream_pipe_read, int n_proc, FILE *ofh) if ( feof(fhs[i]) ) { /* Process died */ finished[i] = 1; + ERROR("EOF during chunk\n"); } else { ERROR("fgets() failed: %s\n", strerror(errno)); } + fprintf(ofh, "Chunk is unfinished!\n"); + chunk_finished = 1; continue; } @@ -646,6 +655,9 @@ static void run_reader(int *stream_pipe_read, int n_proc, FILE *ofh) for ( i=0; i