ralna / CUTEst

The Constrained and Unconstrained Testing Environment with safe threads (CUTEst) for optimization software
Other
83 stars 18 forks source link

Memory leaks in CUTEst #29

Closed jfowkes closed 1 year ago

jfowkes commented 1 year ago

As discovered by a PyCUTEst user, there appear to be memory leaks in CUTEst (accessed via C interface), valgrind reports:

839,988 bytes in 1 blocks are still reachable in loss record 3,062 of 3,064
    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x473D9CD7: __cutest_MOD_cutest_allocate_array_integer (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473DA009: __cutest_MOD_cutest_sparse_hessian_by_rows (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473DEAB5: __cutest_MOD_cutest_size_sparse_hessian (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473B5717: cutest_udimsh_threadsafe_ (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473B5763: cutest_udimsh_ (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473ACD94: cutest__setup (cutestitf.c:460)

959,976 bytes in 1 blocks are still reachable in loss record 3,063 of 3,064
    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x473B1441: cutest_usetup_threadsafe_ (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473B4DF6: cutest_usetup_ (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473ACD09: cutest__setup (cutestitf.c:425)

2,399,992 bytes in 1 blocks are still reachable in loss record 3,064 of 3,064
    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x473E4850: __cutest_MOD_cutest_initialize_workspace (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473B3608: cutest_usetup_threadsafe_ (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473B4DF6: cutest_usetup_ (in /home/jfowkes/pycutest_cache_holder/MODBEALE/_pycutestitf.cpython-310-x86_64-linux-gnu.so)
    by 0x473ACD09: cutest__setup (cutestitf.c:425)

and many more (3,064) such leaks in cutest_usetup and cutest_udimsh (all that I tested).

While this may not seem significant it very quickly adds up when one loops over several different problems!

From the traceback it is clear that these leaks are coming from the Fortran (namely the functions cutest_allocate_array_integer, cutest_usetup_threadsafe, and cutest_initialize_workspace in this snippet) possibly automatic array deallocation is failing for some reason? It is probably worth running both the Fortran and C interfaces to cutest_usetup and cutest_udimsh through valgrind or -fsanitize=address to test for these memory leaks.

nimgould commented 1 year ago

Could I just check ... you are calling uterminate/cterminate at the end of you run. This is supposed to deallocate everything. It is possible, of course, that there is something missing in uterminate, and if so I need to investigate !

jfowkes commented 1 year ago

Ahh the interface is not (looks like this wasn't implemented in the original pycuter) that would explain all the memory leaks!

nimgould commented 1 year ago

Excellent. I hope that does the trick.