Subject: Re: [ruby-ffi] MemoryPointer help |
From: John Croisant |
Date: 12/14/09 2:47 PM |
To: ruby-ffi@googlegroups.com |
On Mon, Dec 14, 2009 at 12:42 PM, Shawn Anderson <shawn42@gmail.com> wrote:
I have a struct that has a void* data, I use MemoryPointer to alloc some mem and assign: mem = FFI::MemoryPointer.new(:long) @struct.data = mem Struct says around in my app, but I was seeing strange GC behavior. Is assigning the mem pointer to the struct good enough to keep it around? I was seeing my struct.data being corrupted over time. adding @mem = mem fixed the problem. Am I using MemoryPointer correctly?
@struct.data = mem doesn't actually store the MemoryPointer instance
in the struct, it just sets @struct.data to the pointer address. That
means the MemoryPointer instance is being garbage collected after it
goes out of scope, and the memory is freed when the MemoryPointer is
garbage collected.
So yes, you should do something like @mem = mem to keep the
MemoryPointer alive until it is supposed be freed. :)
- John