Closed dschopf closed 2 months ago
Running both mpfr_free_cache() and mpfr_free_cache2() seems superfluous.
It should be enough to run mpfr_free_cache2(MPFR_FREE_LOCAL_CACHE) in the threads used by Calculator (or rather in the Thread class), and in mpfr_free_cache() in the Calculator destructor.
I've added mpfr_free_cache() as described above, and fixed other memory leaks, in commit https://github.com/Qalculate/libqalculate/commit/42c5af5333c527c44b67f2d480680029c5284d49. I will therefore close this pull request.
I am sorry but your changes don't fix my issue. I still get the same memory leaks as before your commit, 523 bytes from 23 allocations.
Currently I am only able to reproduce this with my own application, I will post again once I have a minimal working example which shows the same behavior.
Have you tried commit https://github.com/Qalculate/libqalculate/commit/48d1b6d5cd6254e447cd20ea98ac31b22ef6b545 and https://github.com/Qalculate/libqalculate/commit/de0e2bf833e5d2c54c9da0447cee68ecd82897e9?
If your application uses multiple threads (not using the libqalculate Thread class), perhaps you should simple call mpfr_free_cache2(MPFR_FREE_LOCAL_CACHE)
in each. Maybe a Calculator::freeThreadCaches() function should be added.
The potential leaks, caused by not calling mpfr_free_cache, does seem very small (< 1 kB per thread). I do not know to what extent freeing the caches frequently (as would be the cases if done in the Number destructor) would hurt performance (probably not really noticeable either).
Note that libqalculate is generally not thread-safe.
I have tested with 0b02550 and there the issue is still there. However, I can fix it as you suggested, by adding mpfr_free_cache2(MPFR_FREE_LOCAL_CACHE)
before shutting down my threads. So this issue is resolved.
I would also welcome a Calculcator::freeThreadCaches()
, otherwise I also have to link to libmpfr for that single function call.
This fixes a memory leak from the mpfr library:
Not sure if this is the correct fix, especially when the mpfr cache might be used between multiple Numbers.
Calling
in the
Calculator
destructor did not fix the issue, I guess this may be because I use the Calculator from a different thread.