From e3a2807bedf1a1a9e25923ee9bc0db653c4c4033 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 26 Jan 2011 17:57:44 +0100 Subject: Fix many small memory leaks --- src/cell.c | 29 ++++++++++++++++++++++++----- src/cell.h | 2 ++ src/index.c | 8 ++++++-- src/indexamajig.c | 10 ++++++++-- src/mosflm.c | 6 ++++-- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/cell.c b/src/cell.c index 9fa506b0..c3a65066 100644 --- a/src/cell.c +++ b/src/cell.c @@ -81,6 +81,7 @@ UnitCell *cell_new() cell->rep = CELL_REP_CRYST; cell->pointgroup = strdup("1"); + cell->spacegroup = strdup("P 1"); return cell; } @@ -90,6 +91,7 @@ void cell_free(UnitCell *cell) { if ( cell == NULL ) return; free(cell->pointgroup); + free(cell->spacegroup); free(cell); } @@ -187,6 +189,8 @@ UnitCell *cell_new_from_cell(UnitCell *orig) new = malloc(sizeof(UnitCell)); *new = *orig; + cell_set_pointgroup(new, orig->pointgroup); + cell_set_spacegroup(new, orig->spacegroup); return new; } @@ -207,6 +211,20 @@ void cell_set_reciprocal(UnitCell *cell, } +void cell_set_spacegroup(UnitCell *cell, const char *sym) +{ + free(cell->spacegroup); + cell->spacegroup = strdup(sym); +} + + +void cell_set_pointgroup(UnitCell *cell, const char *sym) +{ + free(cell->pointgroup); + cell->pointgroup = strdup(sym); +} + + /************************* Getter helper functions ****************************/ static int cell_crystallographic_to_cartesian(UnitCell *cell, @@ -838,8 +856,7 @@ static void cell_set_pointgroup_from_pdb(UnitCell *cell, const char *sym) if ( strcmp(sym, "P 43 21 2") == 0 ) new = "422"; if ( new != NULL ) { - free(cell->pointgroup); - cell->pointgroup = strdup(new); + cell_set_pointgroup(cell, new); } else { ERROR("Can't determine point group for '%s'\n", sym); } @@ -901,13 +918,15 @@ UnitCell *load_cell_from_pdb(const char *filename) sym = strndup(line+55, 10); notrail(sym); cell_set_pointgroup_from_pdb(cell, sym); - cell->spacegroup = sym; - + cell_set_spacegroup(cell, sym); + free(sym); } else { cell_set_pointgroup_from_pdb(cell, "P 1"); - cell->spacegroup = strdup("P 1"); + cell_set_spacegroup(cell, "P 1"); ERROR("CRYST1 line without space group.\n"); } + + break; /* Done */ } } while ( rval != NULL ); diff --git a/src/cell.h b/src/cell.h index 5e9facb0..17990e2c 100644 --- a/src/cell.h +++ b/src/cell.h @@ -50,6 +50,8 @@ extern void cell_set_parameters(UnitCell *cell, double a, double b, double c, extern void cell_set_cartesian_a(UnitCell *cell, double ax, double ay, double az); extern void cell_set_cartesian_b(UnitCell *cell, double bx, double by, double bz); extern void cell_set_cartesian_c(UnitCell *cell, double cx, double cy, double cz); +extern void cell_set_spacegroup(UnitCell *cell, const char *sym); +extern void cell_set_pointgroup(UnitCell *cell, const char *sym); extern int cell_get_parameters(UnitCell *cell, double *a, double *b, double *c, diff --git a/src/index.c b/src/index.c index ce6021da..6f9b5278 100644 --- a/src/index.c +++ b/src/index.c @@ -162,7 +162,8 @@ void index_pattern(struct image *image, UnitCell *cell, IndexingMethod *indm, } if ( (cellr == CELLR_NONE) || (indm[n] == INDEXING_TEMPLATE) ) { - image->indexed_cell = image->candidate_cells[0]; + image->indexed_cell = cell_new_from_cell( + image->candidate_cells[0]); if ( verbose ) { STATUS("--------------------\n"); STATUS("The indexed cell (matching not" @@ -170,7 +171,7 @@ void index_pattern(struct image *image, UnitCell *cell, IndexingMethod *indm, cell_print(image->indexed_cell); STATUS("--------------------\n"); } - return; + goto done; } for ( i=0; incells; i++ ) { @@ -206,6 +207,7 @@ void index_pattern(struct image *image, UnitCell *cell, IndexingMethod *indm, /* Sanity check */ if ( !peak_sanity_check(image, new_cell, 0, 0.1) ) { STATUS("Failed peak sanity check.\n"); + cell_free(new_cell); continue; } @@ -216,6 +218,7 @@ void index_pattern(struct image *image, UnitCell *cell, IndexingMethod *indm, for ( i=0; incells; i++ ) { cell_free(image->candidate_cells[i]); + image->candidate_cells[i] = NULL; } /* Move on to the next indexing method */ @@ -225,6 +228,7 @@ void index_pattern(struct image *image, UnitCell *cell, IndexingMethod *indm, done: for ( i=0; incells; i++ ) { + /* May free(NULL) if all algorithms were tried */ cell_free(image->candidate_cells[i]); } } diff --git a/src/indexamajig.c b/src/indexamajig.c index 411a1b63..c997ed12 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -461,7 +461,10 @@ static void *get_image(void *qp) } else { rval = fgets(line, 1023, qargs->fh); - if ( rval == NULL ) return NULL; + if ( rval == NULL ) { + free(pargs); + return NULL; + } chomp(line); pargs->filename = malloc(strlen(qargs->prefix)+strlen(line)+1); snprintf(pargs->filename, 1023, "%s%s", qargs->prefix, line); @@ -884,11 +887,14 @@ int main(int argc, char *argv[]) cleanup_indexing(ipriv); + free(indm); + free(ipriv); free(prefix); free(det->panels); free(det); cell_free(cell); - if ( fh != stdout ) fclose(fh); + if ( fh != stdin ) fclose(fh); + if ( ofh != stdout ) fclose(ofh); free(sym); STATUS("There were %i images, of which %i could be indexed.\n", diff --git a/src/mosflm.c b/src/mosflm.c index 5cd2291e..6f27eb7d 100644 --- a/src/mosflm.c +++ b/src/mosflm.c @@ -228,6 +228,8 @@ static void write_spt(struct image *image, const char *filename) } + free(sptlines); + fprintf(fh,"%10.2f %10.2f %10.2f %10.2f %10.2f %10.2f\n", -999.0,-999.0,-999.0,-999.0,-999.0,-999.0); fclose(fh); @@ -262,8 +264,8 @@ static void write_img(struct image *image, const char *filename) /* Header padding */ while ( ftell(fh) < 512 ) fprintf(fh," "); - fwrite(fh, sizeof(unsigned short int), 1, fh); - + fwrite(intimage, sizeof(unsigned short int), 1, fh); + free(intimage); fclose(fh); } -- cgit v1.2.3