Skip to content

Commit

Permalink
Don't call cram_ref_decr on consensus-based references.
Browse files Browse the repository at this point in the history
These are shared refs between containers / slices, but auto-generated
per container.  We set c->ref_free in encode_container, but didn't
check this in all cases.  Additionally fixed memory leaks.

Fixes samtools#1696
  • Loading branch information
jkbonfield committed Nov 16, 2023
1 parent b9e2252 commit b127416
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cram/cram_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,7 @@ static int cram_generate_reference(cram_container *c, cram_slice *s, int r1) {
c->ref = ref;
c->ref_start = ref_start+1;
c->ref_end = ref_end+1;
c->ref_free = 1;

return 0;

Expand Down Expand Up @@ -1836,8 +1837,11 @@ int cram_encode_container(cram_fd *fd, cram_container *c) {
// This starts as 'N' and is amended on-the-fly as we go
// based on MD:Z tags.
if ((c->ref_id = bam_ref(b)) >= 0) {
c->ref_free = 1;
c->ref = NULL;
// c->ref_ref is boolean; whether to free c->ref. In this
// case c->ref will be our auto-embedded sequence.
// Do not confuse with fd->ref_free which is a ptr to free
c->ref_free = 1;
}
}
c->ref_seq_id = c->ref_id;
Expand Down Expand Up @@ -2418,7 +2422,12 @@ int cram_encode_container(cram_fd *fd, cram_container *c) {
c->comp_hdr_block = c_hdr;

if (c->ref_seq_id >= 0) {
cram_ref_decr(fd->refs, c->ref_seq_id);
if (c->ref_free) {
free(c->ref);
c->ref = NULL;
} else {
cram_ref_decr(fd->refs, c->ref_seq_id);
}
}

/* Cache references up-front if we have unsorted access patterns */
Expand Down

0 comments on commit b127416

Please sign in to comment.