问题
I have this toy project:
For the UI I use Vala code that is compiled to C. So I can display a message in UI that comes from Idris.
Both on Idris ans Vala/C side I have this method that sets the pointer to the Idris function. So in Vala code you can see:
global::afni = fn;
that sets a global variable with a pointer to Idris the function. Later in another Vala method I call:
int res = global::afni(0);
string da_label = @"blah $res";
Gtk.Label label = new Gtk.Label (da_label);
So I call comFn
function that is found by pointer stored in global::afni, so far so good, it seems to work with integers.
But how do I make it work with strings?
I have tried various ways to change types and return string as an argument and got errors related to invalid pointers
free(): invalid pointer
and
munmap_chunk(): invalid pointer
回答1:
Vala has the concept of ownership transfer. When a string is returned by a function, Vala assumes that it is responsible for freeing it once it is finished using it. The Idris FFI looks like it allocates all its data inside the VM and manages it forever.
You probably need to tell Vala the return type is unowned string
instead of string
. This will prevent Vala from trying to free it.
来源:https://stackoverflow.com/questions/57810052/idris-how-do-i-call-idris-function-from-vala-c-and-return-a-string-back-to-c-va