Subject: Re: [ruby-ffi] Is there any way we can use string type as return parameter? |
From: Evan Phoenix |
Date: 3/29/10 12:27 PM |
To: ruby-ffi@googlegroups.com |
On Mar 29, 2010, at 10:23 AM, Jesús García Sáez wrote:
Hi, when is it posible to define a "string" type safely? I think, the only situation is for const char * input parameter. Because otherwise we are gonna have memory leaks. For example, for a function like: char *foo(); if we attach it with: attach_function :foo, :foo, [], :string We are gonna have a nice memory leak because nobody is gonna release that string. When we call "foo" method in ruby, we get a String object, we cannot (or I don't know how) get the actual char pointer to release it (either from ruby or with another attached function where we pass the pointer and that function calls free, delete o whatever is necessary).
You use the :strptr type for this. It returns [String, MemoryPointer], so you can do: str, ptr = foo() ptr.free It's really just a simple shortcut for declaring it to be of type :pointer and asking the MemoryPointer to read itself as a String. But because this is a common pattern, we have the :strptr type. - Evan
To unsubscribe from this group, send email to ruby-ffi+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
To unsubscribe from this group, send email to ruby-ffi+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.