MemoryCleaner
MemoryCleaner
manages the cleanup of native memory associated with Proxy
objects.
Key Responsibilities
Maintains a cache of all memory addresses associated with
Proxy
objects (exceptGObject
instances).Releases native memory automatically when owned
Proxy
objects are garbage-collected.Supports different cleanup methods:
Boxed Type Cleanup: Uses
g_boxed_free
for specific data structures.Custom Cleanup: Allows specifying a custom cleanup function.
Fallback Cleanup: Uses
g_free
as a fallback.Enables transferring ownership of native memory to and from native code.
Ownership Semantics
Owned Memory: Automatically cleaned up when the associated
Proxy
object is garbage-collected.Non-Owned Memory: Not automatically cleaned up; must be manually managed or transferred.
Ownership can be managed using takeOwnership and yieldOwnership.
Thread Safety
All operations involving the cache are thread-safe, using a ReentrantLock
to ensure synchronization.
Usage
Register a
Proxy
object using internal mechanisms likegetOrRegister
.Configure cleanup using setFreeFunc for custom functions or setBoxedType for boxed types.
Use takeOwnership to enable automatic cleanup or yieldOwnership to disable it.
Types
Functions
Configures cleanup for a boxed type using g_boxed_free
.
Sets a custom cleanup function for the specified Proxy
instance.
Transfers ownership of the memory to the MemoryCleaner
.
Yields ownership of the memory, preventing automatic cleanup.