Skip to content

Commit

Permalink
fixed memory leak and gcc warnings
Browse files Browse the repository at this point in the history
valgrind now gives a clean bill
  • Loading branch information
lh3 committed May 6, 2015
1 parent 93c68eb commit 163585b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
9 changes: 5 additions & 4 deletions bgt.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ bgt_t *bgt_open(const char *prefix)
void bgt_close(bgt_t *bgt)
{
int i;
if (bgt->b0) bcf_destroy1(bgt->b0);
free(bgt->sub);
if (bgt->h_sub) bcf_hdr_destroy(bgt->h_sub);
hts_itr_destroy(bgt->itr);
hts_idx_destroy(bgt->idx);
bcf_hdr_destroy(bgt->h0);
vcf_close(bgt->bcf);
pbf_close(bgt->pb);
kh_destroy(s2i, bgt->h_samples);
kh_destroy(s2i, (kh_s2i_t*)bgt->h_samples);
for (i = 0; i < bgt->n_samples; ++i)
free(bgt->samples[i]);
free(bgt->samples);
Expand Down Expand Up @@ -92,10 +93,10 @@ void bgt_set_samples(bgt_t *bgt, int n, char *const* samples)
kputs(bgt->samples[bgt->sub[i]], &str);
}
bgt->h_sub->text = str.s;
bgt->h_sub->l_text = str.l;
bgt->h_sub->l_text = str.l + 1; // including the last NULL
bcf_hdr_parse(bgt->h_sub);

t = malloc(bgt->n_sub * 2 * sizeof(int));
t = (int*)malloc(bgt->n_sub * 2 * sizeof(int));
for (i = 0; i < bgt->n_sub; ++i)
t[i<<1|0] = bgt->sub[i]<<1|0, t[i<<1|1] = bgt->sub[i]<<1|1;
pbf_subset(bgt->pb, bgt->n_sub<<1, t);
Expand Down Expand Up @@ -147,7 +148,7 @@ int bgt_read(bgt_t *bgt, bcf1_t *b)
bcf_enc_int1(&b->indiv, id);
pbf_seek(bgt->pb, row);
a = pbf_read(bgt->pb);
gt = calloc(b->n_sample * 2, 4);
gt = (int32_t*)calloc(b->n_sample * 2, 4);
for (i = 0; i < b->n_sample<<1; ++i) {
int g = a[1][i]<<1 | a[0][i];
if (g == 2) gt[i] = 0;
Expand Down
6 changes: 3 additions & 3 deletions pbfview.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char *argv[])
else if (c == 'c') {
if (n_sub == m_sub) {
m_sub = m_sub? m_sub<<1 : 4;
sub = realloc(sub, m_sub * sizeof(int));
sub = (int*)realloc(sub, m_sub * sizeof(int));
}
sub[n_sub++] = atol(optarg);
}
Expand All @@ -46,8 +46,8 @@ int main(int argc, char *argv[])
fscanf(fp, "%s%d%d", magic, &m, &g); // unsafe!!!
if (out_pbf) out = pbf_open_w(0, m, g, shift);
else printf("PIM1 %d %d\n", m, g);
a = calloc(g, sizeof(void*));
for (j = 0; j < g; ++j) a[j] = calloc(m, 1);
a = (uint8_t**)calloc(g, sizeof(void*));
for (j = 0; j < g; ++j) a[j] = (uint8_t*)calloc(m, 1);
while (!feof(fp)) {
for (i = 0; i < m; ++i) {
long x;
Expand Down

0 comments on commit 163585b

Please sign in to comment.