Subject:
Re: [ruby-ffi] Re: Struct/ManagedStruct and GC behaviour
From:
Wayne Meissner
Date:
12/22/09 1:44 PM
To:
ruby-ffi@googlegroups.com

2009/12/23 rogerdpack <rogerpack2005@gmail.com>:
>> 1) Both FFI::Struct and FFI::ManagedStruct are able to be freed by the Ruby impl's GC. Overall GC semantics are no different that any other Ruby object GC'd by the specific Ruby impl.
>
> My question is
> 1) is the "struct's worth of memory" allocated when you call #new

Yes, the backing memory is allocated by Struct.new

>
> 2) when you do struct[:member] = something
>
> does it actually set the bits within the struct right then, or at some
> later marshalling time?

It sets the bits in the struct right then.  The inverse is also true;
x = struct[:member] does a read from the memory, each time you access
it.

> In theory there is a guarantee that it will be collected
> "at_exit" [objects with finalizers are all called in an at_exit in
> MRI].

Yes, although I don't think this same guarantee is made for the jvm.
However, assuming the program is running forever, and you busily
allocate native memory in FFI, and do not explicitly free it,  there
may not be a GC event during the run which frees it.  So you can get
into a state where you exhaust the native memory arena.