Skip to content

Fix multiple memory leaks and issues#806

Merged
dmitry-kabanov merged 45 commits intoMaRDI4NFDI:mainfrom
dmitry-kabanov:c-impl-destructor
Jul 16, 2025
Merged

Fix multiple memory leaks and issues#806
dmitry-kabanov merged 45 commits intoMaRDI4NFDI:mainfrom
dmitry-kabanov:c-impl-destructor

Conversation

@dmitry-kabanov
Copy link
Copy Markdown
Collaborator

@dmitry-kabanov dmitry-kabanov commented Jul 16, 2025

MaRDI Pull Request

This PR was started simply to add destructors to C implementations, so that they can clean allocated memory. However, the actual work done here is larger in focus because much more memory issues were solved.

Changes:

  • Track memory allocations and releases with a global counter NALLOCS_ in util.c with new functions
    oif_util_malloc_ and oif_util_free_
  • In the debug mode, liboif_util.so fails in the destructor, if NALLOCS_ != 0
  • Add functions oif_util_malloc_verbose, oif_util_free_verbose to print verbose debug information
  • Add macros oif_util_malloc, oif_util_free that actually inject debug information (file/function name, line number)
  • Replace function oif_util_str_duplicate with the corresponding macro that injects debug information
  • Add _verbose and _ (non-verbose) variants of the former oif_util_str_duplicate
  • Fix multiple memory leaks in C tests found mostly using LeakSanitizer feature of GCC
  • Run C tests against Python and Julia implementations only in the case of compilation without sanitizers (because they crash with DLLs such as libjulia)
  • Add optional invocation of a function oif_INTERFACE_free (if it exists) so that C implementations can release allocated memory
  • Fix a bag with recreating the global references dictionary in Julia on each load_impl that sometimes led to segmentation fault due to an attempt to remove self variables for implementations when they were already garbage-collected

…anitizers

If a memory leak occurs inside a dynamically loaded implementation,
unloaded the implementation via `dlclose` prevents AddressSanitizer from reporting
correct stack trace.
…izers

AddressSanitizer does not work with embedded Python and Julia.
…f_util_free is requested

Such situation can indicate a real bug or that `oif_util_free` is invoked on memory
that was allocated, e.g., via `malloc` instead of `oif_util_malloc`, hence,
the global number of memory allocations was not increased properly.
Not only loading the functions `setindex!` and `delete!`
on each `load_impl` is wasteful,
but also there was a bug with recreating the global variable `refs_`
each time, which then led sometimes to segmentation faults because
we try to delete variables from the references dictionary that are
not there anymore, because they were in the lost dictionary.
The OIF_FLAG_PRINT_DEBUG_VERBOSE_INFO should be used to decided
if verbose debug information should be printed.
@dmitry-kabanov dmitry-kabanov merged commit d35febd into MaRDI4NFDI:main Jul 16, 2025
7 checks passed
@dmitry-kabanov dmitry-kabanov deleted the c-impl-destructor branch July 16, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant