From 238b6bb3ea8a34bab1c66a005407bbea6953f29b Mon Sep 17 00:00:00 2001 From: Marcus Stoiber Date: Fri, 5 Jul 2019 12:23:53 -0700 Subject: [PATCH] Fix memory leak in mappy.aligner.map. --- python/mappy.pyx | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/python/mappy.pyx b/python/mappy.pyx index f9b9ffe..3273514 100644 --- a/python/mappy.pyx +++ b/python/mappy.pyx @@ -188,23 +188,30 @@ cdef class Aligner: _seq2 = seq2 if isinstance(seq2, bytes) else seq2.encode() regs = cmappy.mm_map_aux(self._idx, _seq, _seq2, &n_regs, b._b, &map_opt) - for i in range(n_regs): - cmappy.mm_reg2hitpy(self._idx, ®s[i], &h) - cigar, _cs, _MD = [], '', '' - for k in range(h.n_cigar32): # convert the 32-bit CIGAR encoding to Python array - c = h.cigar32[k] - cigar.append([c>>4, c&0xf]) - if cs or MD: # generate the cs and/or the MD tag, if requested - if cs: - l_cs_str = cmappy.mm_gen_cs(km, &cs_str, &m_cs_str, self._idx, ®s[i], _seq, 1) - _cs = cs_str[:l_cs_str] if isinstance(cs_str, str) else cs_str[:l_cs_str].decode() - if MD: - l_cs_str = cmappy.mm_gen_MD(km, &cs_str, &m_cs_str, self._idx, ®s[i], _seq) - _MD = cs_str[:l_cs_str] if isinstance(cs_str, str) else cs_str[:l_cs_str].decode() - yield Alignment(h.ctg, h.ctg_len, h.ctg_start, h.ctg_end, h.strand, h.qry_start, h.qry_end, h.mapq, cigar, h.is_primary, h.mlen, h.blen, h.NM, h.trans_strand, h.seg_id, _cs, _MD) - cmappy.mm_free_reg1(®s[i]) - free(regs) - free(cs_str) + try: + i = 0 + while i < n_regs: + cmappy.mm_reg2hitpy(self._idx, ®s[i], &h) + cigar, _cs, _MD = [], '', '' + for k in range(h.n_cigar32): # convert the 32-bit CIGAR encoding to Python array + c = h.cigar32[k] + cigar.append([c>>4, c&0xf]) + if cs or MD: # generate the cs and/or the MD tag, if requested + if cs: + l_cs_str = cmappy.mm_gen_cs(km, &cs_str, &m_cs_str, self._idx, ®s[i], _seq, 1) + _cs = cs_str[:l_cs_str] if isinstance(cs_str, str) else cs_str[:l_cs_str].decode() + if MD: + l_cs_str = cmappy.mm_gen_MD(km, &cs_str, &m_cs_str, self._idx, ®s[i], _seq) + _MD = cs_str[:l_cs_str] if isinstance(cs_str, str) else cs_str[:l_cs_str].decode() + yield Alignment(h.ctg, h.ctg_len, h.ctg_start, h.ctg_end, h.strand, h.qry_start, h.qry_end, h.mapq, cigar, h.is_primary, h.mlen, h.blen, h.NM, h.trans_strand, h.seg_id, _cs, _MD) + cmappy.mm_free_reg1(®s[i]) + i += 1 + finally: + while i < n_regs: + cmappy.mm_free_reg1(®s[i]) + i += 1 + free(regs) + free(cs_str) def seq(self, str name, int start=0, int end=0x7fffffff): cdef int l