class FFI::ManagedStruct
# struct is out of scope, and will be GC’d using DoublyLinkedList#release
end
# do something with the list
ptr = DoublyLinkedList.new(MyLibrary.new_dlist)
begin
end
end
MyLibrary.destroy_dlist(ptr)
def self.release ptr
@@@
end
s.field :tail, :pointer
s.field :head, :pointer
s.include ‘dlist.h’
s.name ‘struct dlist’
struct do |s|
@@@
class DoublyLinkedList < FFI::ManagedStruct
end
attach_function :destroy_dlist, [:pointer], :void
attach_function :new_dlist, [], :pointer
ffi_lib “libmylibrary”
module MyLibrary
Example usage:
invoke the class’s release() method during object finalization.
When the Struct instance is garbage collected, FFI::ManagedStruct will
list or a binary tree.
that has a nontrivial memory management design, such as a linked
The typical use case would be when interacting with a library
FFI::ManagedStruct allows custom garbage-collection of your FFI::Structs.
def initialize(pointer=nil)
ManagedStruct.new
ManagedStruct.new(pointer)
call-seq:
def initialize(pointer=nil) raise NoMethodError, "release() not implemented for class #{self}" unless self.class.respond_to? :release raise ArgumentError, "Must supply a pointer to memory for the Struct" unless pointer super FFI::AutoPointer.new(pointer, self.class.method(:release)) end