libsbml icon indicating copy to clipboard operation
libsbml copied to clipboard

`libsbml.writeMathMLWithNamespaceToString` leaks memory

Open dweindl opened this issue 7 months ago • 5 comments

It looks like libsbml.writeMathMLWithNamespaceToString leaks memory:

$ valgrind --leak-check=full python -c "import libsbml; libsbml.writeMathMLWithNamespaceToString(libsbml.ASTNode(), libsbml.SBMLNamespaces(3, 2))"
# output
==320841== Memcheck, a memory error detector
==320841== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==320841== Using Valgrind-3.24.0 and LibVEX; rerun with -h for copyright info
==320841== Command: python -c import\ libsbml;\ libsbml.writeMathMLWithNamespaceToString(libsbml.ASTNode(),\ libsbml.SBMLNamespaces(3,\ 2))
==320841== 
==320841== 
==320841== HEAP SUMMARY:
==320841==     in use at exit: 483,812 bytes in 20 blocks
==320841==   total heap usage: 65,487 allocs, 65,467 frees, 151,614,594 bytes allocated
==320841== 
==320841== 90 bytes in 1 blocks are definitely lost in loss record 5 of 15
==320841==    at 0x484A858: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==320841==    by 0x9D4621F: safe_strdup (in lib/python3.13/site-packages/libsbml/_libsbml.cpython-313-x86_64-linux-gnu.so)
==320841==    by 0x9D3BF12: writeMathMLWithNamespaceToString (in lib/python3.13/site-packages/libsbml/_libsbml.cpython-313-x86_64-linux-gnu.so)
==320841==    by 0x95DB9CC: _wrap_writeMathMLWithNamespaceToString (in lib/python3.13/site-packages/libsbml/_libsbml.cpython-313-x86_64-linux-gnu.so)
==320841==    by 0x58D527: ??? (in /usr/bin/python3.13)
==320841==    by 0x54AB72: _PyObject_MakeTpCall (in /usr/bin/python3.13)
==320841==    by 0x567183: _PyEval_EvalFrameDefault (in /usr/bin/python3.13)
==320841==    by 0x65B4DC: PyEval_EvalCode (in /usr/bin/python3.13)
==320841==    by 0x67C201: ??? (in /usr/bin/python3.13)
==320841==    by 0x67810D: ??? (in /usr/bin/python3.13)
==320841==    by 0x66A1C5: ??? (in /usr/bin/python3.13)
==320841==    by 0x66A0D0: ??? (in /usr/bin/python3.13)
==320841== 
==320841== LEAK SUMMARY:
==320841==    definitely lost: 90 bytes in 1 blocks
==320841==    indirectly lost: 0 bytes in 0 blocks
==320841==      possibly lost: 0 bytes in 0 blocks
==320841==    still reachable: 483,690 bytes in 18 blocks
==320841==         suppressed: 32 bytes in 1 blocks
==320841== Reachable blocks (those to which a pointer was found) are not shown.
==320841== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==320841== 
==320841== For lists of detected and suppressed errors, rerun with: -s
==320841== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

python-libsbml==5.20.5 with Python 3.13.3 on Ubuntu 25.04.

dweindl avatar May 07 '25 14:05 dweindl

@dweindl agreed, please use writeMathMLToStdString all toString methods from libsbml should be used with caution. That being said, we should annotate the swig bindings for those methods.

fbergmann avatar May 07 '25 15:05 fbergmann

Thanks, Frank. I don't see writeMathMLToStdString in the Python library, though (AttributeError: module 'libsbml' has no attribute 'writeMathMLToStdString').

dweindl avatar May 07 '25 15:05 dweindl

Looking at the code, I think 'writeMathMLToStdString' will also leak, too. But that could be fixed in libsbml; the other leak is probably an issue with SWIG?

EDIT: nope, no leaks with the other version. But it looks like we don't expose it to the python bindings.

luciansmith avatar May 07 '25 19:05 luciansmith

the other leak is probably an issue with SWIG?

Yes, I think %newobject writeMathMLWithNamespaceToString; is missing below https://github.com/sbmlteam/libsbml/blob/31a6ce30cd484fbb8470cb9779c5092a92aa0ddb/src/bindings/swig/libsbml.i#L396.

dweindl avatar May 08 '25 06:05 dweindl

But it looks like we don't expose it to the python bindings.

Yup, #ifndef SWIG: https://github.com/sbmlteam/libsbml/blob/31a6ce30cd484fbb8470cb9779c5092a92aa0ddb/src/sbml/math/MathML.h#L87-L102

dweindl avatar May 08 '25 06:05 dweindl