Subject:
Re: [ruby-ffi] MemoryPointer help
From:
Shawn Anderson
Date:
12/15/09 10:05 AM
To:
ruby-ffi@googlegroups.com

Ok, thanks.  I just wanted to make sure I was using it correctly.

/Shawn

On Mon, Dec 14, 2009 at 3:47 PM, John Croisant <jacius@gmail.com> wrote:
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