Subject: [ruby-ffi] Re: What is the best way to convert a Fixnum to a :pointer? |
From: Wayne Meissner |
Date: 9/16/09 9:32 AM |
To: ruby-ffi@googlegroups.com |
2009/9/16 Brett Blackham <brett.blackham@gmail.com>:
So MemoryPointer is like calling malloc and returning a pointer to it.
Correct. The native memory gets garbage collected for you when the MemoryPointer instance no longer has any hard references to it.
I had tried just sending address to Pointer.new in hopes that it could handle an Int as well as an existing pointer. But it doesn't appear to work.
It works in master, so it must have been added after the 0.4.0 release. Just do the test in ruby land then, and only wrap the address if not already a pointer.
require 'ffi'
=> true
address = FFI::Pointer.new(5)
=> #<Native Pointer address=0x5>
p = FFI::Pointer.new(address)
TypeError: can't convert FFI::Pointer into Integer from (irb):4:in `initialize' from (irb):4:in `new' from (irb):4address = FFI::MemoryPointer :pointer
NoMethodError: undefined method `MemoryPointer' for FFI:Module from (irb):5address = FFI::MemoryPointer.new :pointer
=> #<MemoryPointer address=0x155f140 size=8>
p = FFI::Pointer.new(address)
TypeError: can't convert FFI::MemoryPointer into Integer from (irb):7:in `initialize' from (irb):7:in `new' from (irb):7