Idris: How do I call Idris function from Vala/C and return a string back to C/Vala

≯℡__Kan透↙ 提交于 2019-12-11 00:15:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!