[Vala] Custom array free functions



One of the Xlib functions is this:

Status XQueryTree(Display *display, Window w, Window *root_return,
    Window *parent_return, Window **children_return, unsigned int
    *nchildren_return);

On successful return, children_return is set to point to a new block of
memory that the user's expected to free with XFree() when they're done
with it.

Currently Vala binds it like this:

[CCode (cname = "XQueryTree")]
private void query_tree (Window w, out Window root_return,
    out Window parent_return, out Window[] children_return);

children_return becomes a pair of arguments for the pointer and length.

Unfortunately this means that when Vala's finished with the array it
will then try to free it with g_free(), which won't work (XFree() and
g_free() may use different heaps). Is there any way to tell Vala that it
should use a specific free function for destroying arrays?

I've tried this:

   [CCode (free_function = "XFree")] out Window[] children_return

...but that's just ignored. If I do this:

   out unowned Window[]? children_return

...then Vala will duplicate the array in order to pass it off elsewhere,
which is good, but will naturally not try to free the original array at
all, which is bad.

Right now I've wedged an unpleasantly large chunk of code in the vapi
file that explicitly duplicates the array into a glib one and frees the
original with XFree(), but that seems to be unnecessarily slow and
counter to the Vala spirit. Is there a better way?

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "I have a mind like a steel trap. It's rusty and full of dead mice."
│ --- Anonymous, on rasfc

Attachment: signature.asc
Description: OpenPGP digital signature



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