MemoryManager class

Memory manager for FFI pointer lifecycle management

Provides safe allocation and deallocation of native memory, preventing memory leaks and ensuring proper cleanup.

Dart 3 Compatibility

This class has been updated for Dart 3 FFI compatibility. The allocate method now requires an explicit size parameter because Dart 3's FFI no longer supports generic type size inference with calloc<T>().

Usage:

final ptr = manager.allocate<dpiData>(sizeOf<dpiData>());

Logging

Optional logging can be configured to track memory operations:

MemoryManager.setLogger(
  warning: (msg) => print('[WARNING] $msg'),
  error: (msg) => print('[ERROR] $msg'),
);

Constructors

MemoryManager()

Properties

allocatedCount → int
Get count of currently allocated pointers
no setter
hashCode → int
The hash code for this object.
no setterinherited
isDisposed → bool
Check if manager has been disposed
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

allocate<T extends NativeType>(int sizeInBytes) → Pointer<T>
Allocate memory using calloc and track it for automatic cleanup.
allocateMalloc<T extends NativeType>(int size) → Pointer<T>
Allocate memory using malloc and track it
dispose() → void
Free all allocated memory
free(Pointer<NativeType> pointer) → void
Free a specific pointer and remove from tracking
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

logError(String message) → void
Log an error message (static method for use outside MemoryManager instances)
logWarning(String message) → void
Log a warning message (static method for use outside MemoryManager instances)
setLogger({void warning(String)?, void error(String)?}) → void
Configure logging callbacks for memory operations