Skip to content

Commit 768825f

Browse files
data: fix memory leak when printing data to memory
When print() writes to memory, libyang allocates the output buffer. ly_out_free() is called without the destroy flag, so the buffer stays allocated, and print() copies it into a python string but never frees it. Each call leaks the printed data. Free the buffer after the copy. With a 10 kB leaf printed 10000 times, the RSS growth is: before: 100156 kB after: 0 kB Note that print_mem() and Module.print() already free their buffer. Fixes: 806be4c ("Port to libyang 2") Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 37e1091 commit 768825f

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎libyang/data.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,12 @@ def print(
723723
ret = lib.lyd_print_tree(out_data[0], self.cdata, fmt, flags)
724724

725725
lib.ly_out_free(out_data[0], ffi.NULL, 0)
726-
if ret != lib.LY_SUCCESS:
727-
raise self.context.error("failed to write data")
728-
729-
return c2str(buf[0], decode=True)
726+
try:
727+
if ret != lib.LY_SUCCESS:
728+
raise self.context.error("failed to write data")
729+
return c2str(buf[0], decode=True)
730+
finally:
731+
lib.free(buf[0])
730732

731733
def print_mem(
732734
self,

0 commit comments

Comments
 (0)