From: "peterzhu2118 (Peter Zhu) via ruby-core" Date: 2024-02-23T21:29:05+00:00 Subject: [ruby-core:116917] [Ruby master Feature#20290] Add API for C extensions to free memory Issue #20290 has been updated by peterzhu2118 (Peter Zhu). > Are extensions supposed to undefine the Ruby constants they defined? No. Any Ruby managed objects don���t need to be undefined. > Your implementation seems to imply to me that dfree functions for types owned by the module might still be called after Destruct_ is called, which I think is surprising? I think it makes more sense for the Destruct_ function to be called after all the T_DATA objects are freed so I���ve changed the implementation. > Are Destruct_ methods allowed to call functions which might allocate? (looks like yes?) No. Just like it is not allowed to allocate memory while freeing objects, it is not allowed (or undefined behaviour) to allocate memory in the Destruct_ functions. > What order are Destruct_ methods called in? I would expect inverse order of Init_ methods, but your implementation seems to walk the hash in insertion order? I don���t think extensions should depend on the order in which it is called. There is a deterministic order in which it is called right now, but extensions should not depend on it. > Have you got an example of a particular C extension which needs to free memory in this way? I haven���t looked. But if the C extension does initialization with an external library, then it might need this to release that memory at shutdown. ---------------------------------------- Feature #20290: Add API for C extensions to free memory https://bugs.ruby-lang.org/issues/20290#change-106953 * Author: peterzhu2118 (Peter Zhu) * Status: Open ---------------------------------------- GitHub PR: https://github.com/ruby/ruby/pull/10055 Ticket #19993 added the new feature RUBY_FREE_AT_EXIT, which frees memory in Ruby at shutdown. This allowed tools like Valgrind, ASAN, and macOS leaks to find memory leaks in Ruby without a large number of false-positives outputted. However, this feature is not complete for C extensions, as they may also need to free their memory and there was no way to do so. This means that C extensions might not be able to directly use tools like Valgrind, ASAN, or macOS leaks to find memory leaks. This ticket proposes an API for C extensions to free memory by defining a function called `Destruct_` that is called during shutdown when RUBY_FREE_AT_EXIT is enabled. This name mirrors the `Init_` API that already exists for extension initialization. However, unlike the `Init_` function, `Destruct_` is NOT mandatory for the C extension to implement so that we can preserve backwards compatibility. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/