GNOME Bugzilla – Bug 640786
bad qsort_with_data definition in glib-2.0.vapi
Last modified: 2014-01-29 20:17:18 UTC
Created attachment 179483 [details] [review] qsort_with_data's CompareDataFunc takes pointers instead of the real values Currently glib-2.0.vapi contains the following qsort_with_data definition: private static void qsort_with_data<T> (T[] elems, size_t size, [CCode (type = "GCompareDataFunc")] GLib.CompareDataFunc<T> compare_func); The problem is, that the CompareDataFunc does get a pointer to the actual value, thus the following statement would be correct: private static void qsort_with_data<T> (T[] elems, size_t size, [CCode (type = "GCompareDataFunc")] GLib.CompareDataFunc<T*> compare_func); You can see the problem in this example - currently you need to use it like this (you will get some gcc warnings, but the code sorts the array): int compare(int a, int b) { return (*((int*) a) - *((int *)b)) < 0 ? -1 : 1; } void main() { int[] testdata = {5,4,3,2,1}; qsort_with_data<int>(testdata, sizeof(int), compare ); foreach(int i in testdata) stdout.printf("%d, ", i); stdout.printf("\n"); } After changing the qsort_with_data in the vapi file you can do it like this: int compare(int *a, int *b) { return (*a - *b) < 0 ? -1 : 1; } void main() { int[] testdata = {5,4,3,2,1}; qsort_with_data<int>(testdata, sizeof(int), compare ); foreach(int i in testdata) stdout.printf("%d, ", i); stdout.printf("\n"); } Still remaining are some of the gcc warnings. I'm not sure were these needs to be fixed. They are printed because of a missing typecast in the generated C code (gconstpointer to gint* in this example).
commit 994dd5674637dfe577408f486216728addbae8e5 Author: Evan Nemerson <evan@coeus-group.com> Date: Tue Jan 28 21:56:03 2014 -0800 glib-2.0: fix qsort_with_data binding Fixes bug 640786.
*** Bug 686338 has been marked as a duplicate of this bug. ***