Skip to content

Commit

Permalink
- Fixed memory leak for cython disasm functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jchristman authored and aquynh committed Jan 16, 2015
1 parent f2157de commit 50c3823
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions bindings/python/pyx/ccapstone.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,18 @@ cdef class Cs(object):
detail = self._cs.detail
arch = self._cs.arch

for i from 0 <= i < res:
if detail:
dummy = CsInsn(CsDetail(arch, <size_t>allinsn[i].detail))
else:
dummy = CsInsn(None)
try:
for i from 0 <= i < res:
if detail:
dummy = CsInsn(CsDetail(arch, <size_t>allinsn[i].detail))
else:
dummy = CsInsn(None)

dummy._raw = allinsn[i]
dummy._csh = self.csh
yield dummy

cc.cs_free(allinsn, res)
dummy._raw = allinsn[i]
dummy._csh = self.csh
yield dummy
finally:
cc.cs_free(allinsn, res)


# Light function to disassemble binary. This is about 20% faster than disasm() because
Expand All @@ -299,11 +300,12 @@ cdef class Cs(object):

cdef res = cc.cs_disasm(self.csh, code, len(code), addr, count, &allinsn)

for i from 0 <= i < res:
insn = allinsn[i]
yield (insn.address, insn.size, insn.mnemonic, insn.op_str)

cc.cs_free(allinsn, res)
try:
for i from 0 <= i < res:
insn = allinsn[i]
yield (insn.address, insn.size, insn.mnemonic, insn.op_str)
finally:
cc.cs_free(allinsn, res)


# print out debugging info
Expand Down

0 comments on commit 50c3823

Please sign in to comment.