aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2011-01-26 17:57:44 +0100
committerThomas White <taw@physics.org>2012-02-22 15:27:12 +0100
commite3a2807bedf1a1a9e25923ee9bc0db653c4c4033 (patch)
treec8bb22ff1ab2b38e37fa70d545c6bdcc0cf0e7b7 /src
parenteb24fd94de4e5d59b691acf0b1bfd43de64d66c1 (diff)
Fix many small memory leaks
Diffstat (limited to 'src')
-rw-r--r--src/cell.c29
-rw-r--r--src/cell.h2
-rw-r--r--src/index.c8
-rw-r--r--src/indexamajig.c10
-rw-r--r--src/mosflm.c6
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; i<image->ncells; 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; i<image->ncells; 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; i<image->ncells; 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);
}