Re: [Vala] GLib.List



Hello,
(my comments are inline, and please keep the discussion on list)

2011/5/19 Jan Spurný <JSpurny seznam cz>:
Hi,

thanks again for your reply,

 >> Did you have any problem with the following? it should be the first
 >> thing you try (and is likely the correct thing):
 >>
 >> [CCode (cheader_filename = "oldlib.h")]
 >> public static GLib.List<X> old_lib_make_x_list();
 >
 > Thanks for your reply, but unfortunately, it really WAS the first thing I did
 > - but because X is not reference counted, it resulted in errors like this:
 >
 > oldlib.vapi:61.33-61.36: error: duplicating X instance, use unowned variable
 or explicitly invoke copy method
 >        public static GLib.List<X> old_lib_make_x_list();
 >                                ^

 Yeah, this error message is misleading. You should put unowned in your
 loop and not in the bindings, i.e.

 foreach(unowned X myx in old_lib_make_x_list()) {
     ...
 }

ok, that makes sense.. unfortunately, it won't help me much - it's probably my
fault, I should've explained it better - I'm using GLib.List<X> returned from
Are you sure you aren't iterating over the returned list anywhere? The
error message you got suggests you are. (but maybe I'm mistaken)

oldlib almost exclusively only to pass it to another oldlib function, so what
I really need to do is something like this:

void vala_func() {
 var list = old_lib_make_x_list();
 var list2 = old_lib_filter_list(list);
 var list3 = old_lib_obscure_function_xy(list, list2);
 old_lib_supercomplicated_function(list2, list3);
}

which in c (c99) I would write like this:

void c_func()
{
 GList *list = old_lib_make_x_list();
 GList *list2 = old_lib_filter_list(list);
 GList *list3 = old_lib_obscure_function_xy(list, list2);
 old_lib_supercomplicated_function(list2, list3);

 // old_lib_destroy_x_list destroys the list (by a call to g_list_free) but
 // also the contents (by a call to x_destroy for each element)
 old_lib_destroy_x_list(list);
 old_lib_destroy_x_list(list2);
 old_lib_destroy_x_list(list3);
}

This should be exactly the the expected result (that is, if you don't
mind that valac uses __g_list_free__x_destroy_ instead of
old_lib_destroy_x_list)


So I need some way how to get the return value of old_lib list generating
functions and at the same time to make sure the list gets destroyed..

In general, you shouldn't worry much about memory management: if this
is something that is normally done in Vala (in your example, the user
should free both the list and its contents), just declare it normally.

Please tell me if this is confusing, I'm not really good in english, so if
this doesn't make sense, I'll try to write some minimal piece of code which
could demonstrate my problem more clearly.

Your english is fine :-) And yes, a minimal reproduction of your
problem would really help.

Regards,
Abderrahim



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