Skip to content

Commit 651a936

Browse files
rpetrovskijkbonfield
authored andcommitted
Memory leak iterating multiple queries over cram
The following code consumes memory indefinitely. Memory leak is gone once the change is applied. Steps: 1. build htslib 2. compile test.c with: gcc -O0 -ggdb -I htslib/install/include test.c -L htslib/install/lib/ -l:libhts.a -lz -lpthread -llzma -lbz2 3. run ./a.out some.cram chr1 4. watch virtual memory going up in top 5. apply patch, rebuild test.c, notice virtual memory does not change test.c: ```c++ int main(int argc,char** argv) { hts_itr_t *iter=NULL; hts_idx_t *idx=NULL; samFile *in = NULL; bam1_t *b= NULL; bam_hdr_t *header = NULL; if(argc!=3) return -1; in = sam_open(argv[1], "r"); if(in==NULL) return -1; if ((header = sam_hdr_read(in)) == 0) return -1; idx = sam_index_load(in, argv[1]); if(idx==NULL) return -1; b = bam_init1(); fputs("reading\n",stdout); do { if (iter) hts_itr_destroy(iter); iter = sam_itr_querys(idx, header, argv[2]); if(!iter) return -1; // fputs("DO STUFF\n",stdout); } while (sam_itr_next(in, iter, b) >= 0); fputs("done reading\n",stdout); hts_itr_destroy(iter); bam_destroy1(b); hts_idx_destroy(idx); bam_hdr_destroy(header); sam_close(in); return 0; } ```
1 parent 816a220 commit 651a936

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

cram/cram_io.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,10 +2747,17 @@ void cram_free_container(cram_container *c) {
27472747
cram_free_block(c->comp_hdr_block);
27482748

27492749
if (c->slices) {
2750-
for (i = 0; i < c->max_slice; i++)
2751-
if (c->slices[i])
2752-
cram_free_slice(c->slices[i]);
2753-
free(c->slices);
2750+
for (i = 0; i < c->max_slice; i++) {
2751+
if (c->slices[i])
2752+
cram_free_slice(c->slices[i]);
2753+
if (c->slices[i] == c->slice)
2754+
c->slice = NULL;
2755+
}
2756+
free(c->slices);
2757+
}
2758+
2759+
if (c->slice) {
2760+
cram_free_slice(c->slice);
27542761
}
27552762

27562763
for (id = DS_RN; id < DS_TN; id++)
@@ -2885,6 +2892,7 @@ cram_container *cram_read_container(cram_fd *fd) {
28852892

28862893
c->offset = rd;
28872894
c->slices = NULL;
2895+
c->slice = NULL;
28882896
c->curr_slice = 0;
28892897
c->max_slice = c->num_landmarks;
28902898
c->slice_rec = 0;
@@ -3129,14 +3137,20 @@ static int cram_flush_result(cram_fd *fd) {
31293137
return -1;
31303138

31313139
/* Free the container */
3132-
for (i = 0; i < c->max_slice; i++) {
3133-
if (c->slices && c->slices[i]) {
3134-
cram_free_slice(c->slices[i]);
3140+
if (c->slices) {
3141+
for (i = 0; i < c->max_slice; i++) {
3142+
if (c->slices[i])
3143+
cram_free_slice(c->slices[i]);
3144+
if (c->slices[i] == c->slice)
3145+
c->slice = NULL;
31353146
c->slices[i] = NULL;
31363147
}
31373148
}
31383149

3139-
c->slice = NULL;
3150+
if (c->slice) {
3151+
cram_free_slice(c->slice);
3152+
c->slice = NULL;
3153+
}
31403154
c->curr_slice = 0;
31413155

31423156
// Our jobs will be in order, so we free the last

0 commit comments

Comments
 (0)