Fri, 17 Dec 2010 01:30:02 +0900

Ruby-FFI 1.0.0 からは Ruby 1.9.2 以降が必要になった

% sudo gem1.8 install ffi
Password:
ERROR:  Error installing ffi:
        ffi requires Ruby version >= 1.9.2

そのため、Ruby 1.8 向けの gem をインストールするときはバージョン 0.6.3 を指定する。

% sudo gem1.8 install ffi --version "= 0.6.3"
Password:
Building native extensions.  This could take a while...
Successfully installed ffi-0.6.3
1 gem installed
Installing ri documentation for ffi-0.6.3...
Installing RDoc documentation for ffi-0.6.3...

Sat, 11 Dec 2010 23:21:58 +0900

Ruby FFI のメモリ管理についてメモ。FFI::MemoryPointer で確保したメモリを自分で破棄する場合は、autorelease を false にする。

error   = FFI::MemoryPointer.new(:pointer)
...
C.LLVMDisposeMessage(error)
error.autorelease=false

そうしないと、FFI::MemoryPointer は GC 時に確保したメモリを破棄しようとする (MemoryPointer.c#L138) ので、たとえば Mac OS X では以下のようなエラーが出る。

ruby(891,0x7fff70874ca0) malloc: *** error for object 0x102e8fb10: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

エラー処理とかでは割と忘れがちかもしれない。

Want fries with that?

Open Source Projects