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 (except GObject 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

Types

Link copied to clipboard

Default implementation of native cleanup functions for production use.

Functions

Link copied to clipboard
fun free(cPointer: <Error class: unknown class>?)

Manually frees the memory associated with the specified address.

Link copied to clipboard
fun setBoxedType(proxy: Proxy, boxedType: <Error class: unknown class>)

Configures cleanup for a boxed type using g_boxed_free.

Link copied to clipboard
fun setFreeFunc(proxy: Proxy, freeFunc: (<Error class: unknown class>?) -> Unit)

Sets a custom cleanup function for the specified Proxy instance.

Link copied to clipboard
fun takeOwnership(proxy: Proxy)

Transfers ownership of the memory to the MemoryCleaner.

Link copied to clipboard
fun yieldOwnership(proxy: Proxy)

Yields ownership of the memory, preventing automatic cleanup.