Skip to content

Commit 37e1091

Browse files
data: fix memory leak when reading anydata values
lyd_any_value_str() returns a string allocated by libyang, and the caller must free it. value() copies it into a python string but never frees it, so each call leaks the printed content. Free it after the copy. With a 10 kB anyxml value read 10000 times, the RSS growth is: before: 97812 kB after: 0 kB Fixes: 806be4c ("Port to libyang 2") Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent bd2bb52 commit 37e1091

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎libyang/data.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,10 @@ def value(self, fmt: str = "xml"):
11751175
ret = lib.lyd_any_value_str(self.cdata, data_format(fmt), anystr)
11761176
if ret != lib.LY_SUCCESS:
11771177
raise self.context.error("cannot get data")
1178-
return c2str(anystr[0])
1178+
try:
1179+
return c2str(anystr[0])
1180+
finally:
1181+
lib.free(anystr[0])
11791182

11801183

11811184
# -------------------------------------------------------------------------------------
@@ -1186,7 +1189,10 @@ def value(self, fmt: str = "xml"):
11861189
ret = lib.lyd_any_value_str(self.cdata, data_format(fmt), anystr)
11871190
if ret != lib.LY_SUCCESS:
11881191
raise self.context.error("cannot get data")
1189-
return c2str(anystr[0])
1192+
try:
1193+
return c2str(anystr[0])
1194+
finally:
1195+
lib.free(anystr[0])
11901196

11911197

11921198
# -------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)