From 274a5e2803eb3afe0d53b27c6078ef4495d587a0 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 17 Mar 2010 14:09:49 +0100 Subject: Fix various memory leaks and tidy a few things --- src/cell.c | 16 ++++++++++++++++ src/cell.h | 1 + src/dirax.c | 1 + src/indexamajig.c | 17 +++++++++++++---- src/sfac.c | 36 ++++++++++++++++++++++++++++++++++++ src/sfac.h | 3 +++ 6 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/cell.c b/src/cell.c index 5747ed63..408b5bf3 100644 --- a/src/cell.c +++ b/src/cell.c @@ -262,11 +262,25 @@ static UnitCell *cell_new_from_axes(struct rvec as, struct rvec bs, cell->cy = gsl_matrix_get(inv, 2, 1); cell->cz = gsl_matrix_get(inv, 2, 2); + gsl_matrix_free(inv); + cell_update_crystallographic(cell); return cell; } +UnitCell *cell_new_from_cell(UnitCell *orig) +{ + UnitCell *new; + + new = malloc(sizeof(UnitCell)); + + *new = *orig; + + return new; +} + + void cell_get_cartesian(UnitCell *cell, double *ax, double *ay, double *az, double *bx, double *by, double *bz, @@ -321,6 +335,8 @@ void cell_get_reciprocal(UnitCell *cell, *asz = gsl_matrix_get(inv, 2, 0); *bsz = gsl_matrix_get(inv, 2, 1); *csz = gsl_matrix_get(inv, 2, 2); + + gsl_matrix_free(inv); } diff --git a/src/cell.h b/src/cell.h index 4c74b283..5a534268 100644 --- a/src/cell.h +++ b/src/cell.h @@ -39,6 +39,7 @@ typedef struct { } UnitCell; extern UnitCell *cell_new(void); +extern UnitCell *cell_new_from_cell(UnitCell *orig); /* Lengths in m, angles in radians */ extern UnitCell *cell_new_from_parameters(double a, double b, double c, diff --git a/src/dirax.c b/src/dirax.c index d18d1fa6..49e16921 100644 --- a/src/dirax.c +++ b/src/dirax.c @@ -421,6 +421,7 @@ void run_dirax(struct image *image) close(image->dirax_pty); close(saved_stderr); + free(image->dirax_rbuffer); wait(&status); return; diff --git a/src/indexamajig.c b/src/indexamajig.c index f3cb05fc..7c227cb1 100644 --- a/src/indexamajig.c +++ b/src/indexamajig.c @@ -114,6 +114,7 @@ static struct image *get_simage(struct image *template, int alternate) panels[1].res = 13333.3; /* 75 microns/pixel */ image->det.panels = panels; + } else { /* Copy pointer to old geometry */ @@ -123,8 +124,9 @@ static struct image *get_simage(struct image *template, int alternate) image->lambda = ph_en_to_lambda(eV_to_J(1.8e3)); - image->molecule = template->molecule; - image->molecule->cell = template->indexed_cell; + image->molecule = copy_molecule(template->molecule); + free(image->molecule->cell); + image->molecule->cell = cell_new_from_cell(template->indexed_cell); return image; } @@ -241,8 +243,9 @@ int main(int argc, char *argv[]) if ( indm_str == NULL ) { STATUS("You didn't specify an indexing method, so I won't" - " try to index anything. If that isn't what you\n" - " wanted, re-run with --indexing=.\n"); + " try to index anything.\n" + "If that isn't what you wanted, re-run with" + " --indexing=.\n"); indm = INDEXING_NONE; } else if ( strcmp(indm_str, "none") == 0 ) { indm = INDEXING_NONE; @@ -252,6 +255,7 @@ int main(int argc, char *argv[]) ERROR("Unrecognised indexing method '%s'\n", indm_str); return 1; } + free(indm_str); image.molecule = load_molecule(); if ( image.molecule == NULL ) { @@ -342,8 +346,12 @@ int main(int argc, char *argv[]) /* Finished with alternate image */ if ( simage->twotheta != NULL ) free(simage->twotheta); if ( simage->data != NULL ) free(simage->data); + free_molecule(simage->molecule); free(simage); + /* Only free cell if found */ + free(image.indexed_cell); + done: free(image.data); free(image.det.panels); @@ -354,6 +362,7 @@ done: } while ( rval != NULL ); fclose(fh); + free_molecule(image.molecule); STATUS("There were %i images.\n", n_images); STATUS("%i hits were found.\n", n_hits); diff --git a/src/sfac.c b/src/sfac.c index 0e8b9382..81e381b3 100644 --- a/src/sfac.c +++ b/src/sfac.c @@ -323,6 +323,7 @@ struct molecule *load_molecule() if ( mol == NULL ) return NULL; mol->n_species = 0; mol->reflections = NULL; + mol->cell = NULL; fh = fopen("molecule.pdb", "r"); if ( fh == NULL ) { @@ -443,6 +444,41 @@ struct molecule *load_molecule() } +void free_molecule(struct molecule *mol) +{ + int i; + + for ( i=0; in_species; i++ ) { + free(mol->species[i]); + } + + free(mol->cell); + free(mol); +} + + +struct molecule *copy_molecule(struct molecule *orig) +{ + struct molecule *new; + int i; + + new = malloc(sizeof(*new)); + *new = *orig; + + /* Now sort out pointers */ + for ( i=0; in_species; i++ ) { + new->species[i] = malloc(sizeof(struct mol_species)); + memcpy(new->species[i], orig->species[i], + sizeof(struct mol_species)); + } + + new->cell = cell_new_from_cell(orig->cell); + new->reflections = NULL; + + return new; +} + + double complex *get_reflections(struct molecule *mol, double en) { double complex *reflections; diff --git a/src/sfac.h b/src/sfac.h index d1424fc7..000b859f 100644 --- a/src/sfac.h +++ b/src/sfac.h @@ -54,7 +54,10 @@ struct molecule extern double complex get_sfac(const char *n, double s, double en); extern struct molecule *load_molecule(void); +extern void free_molecule(struct molecule *mol); +extern struct molecule *copy_molecule(struct molecule *orig); extern double complex *get_reflections(struct molecule *mol, double en); extern void get_reflections_cached(struct molecule *mol, double en); + #endif /* SFAC_H */ -- cgit v1.2.3