[Vala] GLib.List



Hi,

I'm writing a new library in vala, and that library uses our old library which is written in c. Until now I 
was able to write VAPI files for everything in the old library, but now I stumbled on a big problem - some 
functions in the old library returns GLib.List with some dynamicaly allocated structs ("Compact class in vala 
terminology) as data members. Something like this:

typedef struct {
    ..
    ..
} X;

X    *x_new();
void x_destroy(X* x);

GList old_lib_make_x_list()
{
    GList *result = NULL;
    result = g_list_prepend(result, x_new());
    return result;
}

in the old c code, I was using function similar to the one below to destroy the whole list:

void old_lib_destroy_x_list(GList *xs)
{
    void helper_x_destroy(gpointer data, gpointer user_data) {
        x_destroy((X*)data);
    }
    
    g_list_foreach(xs, helper_x_destroy, NULL);
    g_list_free(xs);
}

But now I'm trying to use the result of "old_lib_make_x_list" in vala and there it all fails.

The VAPI file looks something like this:


[Compact]
[CCode (cheader_filename = "x.h", free_function = "x_destroy")]
public class X {
    public X();
    ..
}

[CCode (cheader_filename = "oldlib.h")]
public static GLib.List<unowned X>? old_lib_make_x_list();

[CCode (cheader_filename = "oldlib.h")]
public static void old_lib_destroy_x_list(GLib.List<unowned X>? xs);


Now I have probably two, equaly wrong possibilities. Either let vala manage things (it can't of course..):

void somefunc() {
   var list = old_lib_make_x_list();
   ..
   <use list>
   ..
}

 - it compiles
 - it works
 - but it never free "X" objects

or I can try to free it myself (which I shouldn't of course..):

void somefunc() {
   var list = old_lib_make_x_list();
   ..
   <use list>
   ..
   old_lib_destroy_x_list(list);
}

 - it compiles
 - it works (before it segfaults)
 - it segfaults because vala tries to free already freed GLib.List
   at the end of "somefunc"


I tried almost everything, but it looks like I have only the following options:

 1) rewrite oldlib with Gee.ArrayList
     - it would take too much time, because GList is used heavily in oldlib..
     - it would break oldlib interface, meaning we would have to rewrite
       every project using oldlib
     - only god knows how many new errors this would introduce into a stable
       and well tested library

 2) stop using vala in our projects and go back to plain c or maybe c++
     - it would take too much time, because a lot of good, well tested code
       is already written in vala, waiting only for oldlib VAPI binding..
     - good c developers with are not easy to found..
     - good c++ developers are expensive (many java or c# developers are quite
       comfortable in vala, provided they do not write VAPI files..)
     - I hate c++

 3) kill myself :)


I would greatly appreciate any help or suggestions..



Regards,
Jan Spurny




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]