diff options
author | Thomas White <taw@bitwiz.org.uk> | 2012-07-11 22:54:53 +0200 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2012-07-11 22:54:53 +0200 |
commit | 849df8b6dd7de1d45eaf8af1f4f2b1c1d72e5c9c (patch) | |
tree | 641d3211bc42fd0ce2ab8c6009f9eff0e76f76b8 | |
parent | 0fe6353366b4175b63739f5da63c1cef2c2b74dd (diff) |
Fix a load of memory leaks
-rw-r--r-- | libcrystfel/src/peaks.c | 32 | ||||
-rw-r--r-- | libcrystfel/src/reax.c | 7 | ||||
-rw-r--r-- | 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; i<s->n_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<n_proc; i++ ) { fclose(fhs[i]); } + free(fhs); + + if ( ofh != stdout ) fclose(ofh); } @@ -892,16 +904,14 @@ int main(int argc, char *argv[]) free(filename); if ( outfile == NULL ) { - outfile = strdup("-"); - } - if ( strcmp(outfile, "-") == 0 ) { ofh = stdout; } else { ofh = fopen(outfile, "w"); - } - if ( ofh == NULL ) { - ERROR("Failed to open output file '%s'\n", outfile); - return 1; + if ( ofh == NULL ) { + ERROR("Failed to open output file '%s'\n", outfile); + return 1; + } + free(outfile); } if ( hdf5_peak_path == NULL ) { @@ -1064,6 +1074,7 @@ int main(int argc, char *argv[]) prepare_line = tmp; } snprintf(prepare_filename, 1023, "%s%s", prefix, prepare_line); + free(prepare_line); /* Prepare the indexer */ if ( indm != NULL ) { @@ -1144,12 +1155,6 @@ int main(int argc, char *argv[]) return 1; } - finished = calloc(n_proc, sizeof(int)); - if ( finished == NULL ) { - ERROR("Couldn't allocate memory for process flags.\n"); - return 1; - } - for ( i=0; i<n_proc; i++ ) { int stream_pipe[2]; @@ -1170,7 +1175,44 @@ int main(int argc, char *argv[]) return 1; } - if ( pr == 0 ) run_reader(stream_pipe_read, n_proc, ofh); + if ( pr == 0 ) { + + /* Free resources not needed by reader + * (but which will be needed by worker or master) */ + for ( i=0; i<n_proc; i++ ) { + close(stream_pipe_write[i]); + } + free(prefix); + free(use_this_one_instead); + free(filename_pipes); + free(result_fhs); + free(stream_pipe_write); + cleanup_indexing(ipriv); + free(indm); + free(ipriv); + free_detector_geometry(det); + free(beam); + free(element); + free(hdf5_peak_path); + free_copy_hdf5_field_list(copyme); + cell_free(cell); + free(pids); + fclose(fh); + + run_reader(stream_pipe_read, n_proc, ofh); + + free(stream_pipe_read); + + exit(0); + + } + + /* Free resources needed by reader only */ + if ( ofh != stdout ) fclose(ofh); + for ( i=0; i<n_proc; i++ ) { + close(stream_pipe_read[i]); + } + free(stream_pipe_read); /* Fork the right number of times */ for ( i=0; i<n_proc; i++ ) { @@ -1197,19 +1239,34 @@ int main(int argc, char *argv[]) if ( p == 0 ) { - FILE *fh; + FILE *sfh; + int j; + + /* Free resources which will not be needed by worker */ + for ( j=0; j<n_proc; j++ ) { + if ( i != j ) close(stream_pipe_write[j]); + } + free(prefix); + free(use_this_one_instead); + free(filename_pipes); + free(result_fhs); + fclose(fh); + free(pids); /* Child process gets the 'read' end of the filename * pipe, and the 'write' end of the result pipe. */ close(filename_pipe[1]); close(result_pipe[0]); - fh = fdopen(stream_pipe_write[i], "w"); + sfh = fdopen(stream_pipe_write[i], "w"); run_work(&iargs, filename_pipe[0], result_pipe[1], - fh, i); - fclose(fh); + sfh, i); + fclose(sfh); + + free(stream_pipe_write); exit(0); + } /* Parent process gets the 'write' end of the filename pipe @@ -1227,6 +1284,21 @@ int main(int argc, char *argv[]) } + /* Free resources which will not be used by the main thread */ + cleanup_indexing(ipriv); + free(indm); + free(ipriv); + free_detector_geometry(det); + free(beam); + free(element); + free(hdf5_peak_path); + free_copy_hdf5_field_list(copyme); + cell_free(cell); + for ( i=0; i<n_proc; i++ ) { + close(stream_pipe_write[i]); + } + free(stream_pipe_write); + /* Send first image to all children */ for ( i=0; i<n_proc; i++ ) { @@ -1251,6 +1323,12 @@ int main(int argc, char *argv[]) } + finished = calloc(n_proc, sizeof(int)); + if ( finished == NULL ) { + ERROR("Couldn't allocate memory for process flags.\n"); + return 1; + } + allDone = 0; while ( !allDone ) { @@ -1361,6 +1439,8 @@ int main(int argc, char *argv[]) } + fclose(fh); + for ( i=0; i<n_proc; i++ ) { int status; waitpid(pids[i], &status, 0); @@ -1371,28 +1451,12 @@ int main(int argc, char *argv[]) fclose(result_fhs[i]); } + free(prefix); free(filename_pipes); free(result_fhs); - free(stream_pipe_read); - free(stream_pipe_write); free(pids); free(finished); - cleanup_indexing(ipriv); - - free(indm); - free(ipriv); - free(prefix); - free_detector_geometry(det); - free(beam); - free(element); - free(hdf5_peak_path); - free_copy_hdf5_field_list(copyme); - cell_free(cell); - free(use_this_one_instead); - if ( fh != stdin ) fclose(fh); - if ( ofh != stdout ) fclose(ofh); - STATUS("There were %i images, of which %i could be indexed.\n", n_processed, n_indexable); |