Skip to content

Commit b3c7a61

Browse files
committed
Decouple htsFile::fnidx from its source and let HTSlib manage its memory.
1 parent 180721b commit b3c7a61

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

hts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,7 @@ int hts_close(htsFile *fp)
12311231
hts_idx_destroy(fp->idx);
12321232
free(fp->fn);
12331233
free(fp->fn_aux);
1234+
free(fp->fnidx);
12341235
free(fp->line.s);
12351236
free(fp);
12361237
errno = save;

htslib/hts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ typedef struct {
238238
void *state; // format specific state information
239239
htsFormat format;
240240
hts_idx_t *idx;
241-
const char *fnidx;
241+
char *fnidx;
242242
struct sam_hdr_t *bam_header;
243243
} htsFile;
244244

sam.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,9 @@ int bam_index_build(const char *fn, int min_shift)
894894
// Initialise fp->idx for the current format type.
895895
// This must be called after the header has been written but no other data.
896896
int sam_idx_init(htsFile *fp, sam_hdr_t *h, int min_shift, const char *fnidx) {
897-
fp->fnidx = fnidx;
897+
fp->fnidx = strdup(fnidx);
898+
if (!fp->fnidx) return -1;
899+
898900
if (fp->format.format == bam || fp->format.format == bcf ||
899901
(fp->format.format == sam && fp->format.compression == bgzf)) {
900902
int n_lvls, fmt = HTS_FMT_CSI;

vcf.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,7 +3295,8 @@ static int vcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fn
32953295
fp->idx = NULL;
32963296
return -1;
32973297
}
3298-
fp->fnidx = fnidx;
3298+
fp->fnidx = strdup(fnidx);
3299+
if (!fp->fnidx) return -1;
32993300

33003301
return 0;
33013302
}
@@ -3315,7 +3316,8 @@ int bcf_idx_init(htsFile *fp, bcf_hdr_t *h, int min_shift, const char *fnidx) {
33153316

33163317
fp->idx = hts_idx_init(nids, HTS_FMT_CSI, bgzf_tell(fp->fp.bgzf), min_shift, n_lvls);
33173318
if (!fp->idx) return -1;
3318-
fp->fnidx = fnidx;
3319+
fp->fnidx = strdup(fnidx);
3320+
if (!fp->fnidx) return -1;
33193321

33203322
return 0;
33213323
}

0 commit comments

Comments
 (0)