Subject:
Re: [ruby-ffi] why FFI::MemoryPointer is autoconverted into FFI::Pointer in a FFI::Struct?
From:
Chuck Remes
Date:
3/18/11 7:43 AM
To:
ruby-ffi@googlegroups.com


On Mar 18, 2011, at 5:02 AM, Jesús GS wrote:

> Hi,
> 
> I'm really confuse with this behaviour. I think I'm missing some
> concept of memory management of ffi.
> 
> Let's imagine a C library with something like:
> 
> struct foo
> {
>    void *p;
> };
> void some_function( struct foo * in_parameter );
> 
> From ruby we are gonna create that struct before call the function,
> with something similar to:
>>> require 'ffi'
> => true
>>> class Foo < FFI::Struct; layout :p, :pointer; end
> => #<FFI::StructLayout:0x1f0f0c8>
>>> foo = Foo.new
> => #<Foo:0x1a15597>
>>> foo[:p] = my_pointer = FFI::MemoryPointer.new(:int, 100)
> => #<MemoryPointer address=0xb3c06380 size=400>
>>> foo[:p]
> => #<Pointer address=0xb3c06380>
>>> my_pointer
> => #<MemoryPointer address=0xb3c06380 size=400>
> 
> 
> I cannot free a Pointer (only MemoryPointer), so what should I do?
> Create a free_struct_foo( void * p ); function in C? Ignore it because
> I don't need to free that allocated memory or just hope no memory
> leaks will happen?.

I handle a similar situation by calling #attach_function on LibC's free function.

cr