]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
glib-2.0: Add Array.remove*() wrapper to avoid leaking generic elements
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 27 Jul 2018 14:25:44 +0000 (16:25 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Fri, 27 Jul 2018 14:25:44 +0000 (16:25 +0200)
tests/Makefile.am
tests/basic-types/garray.vala [new file with mode: 0644]
vapi/glib-2.0.vapi

index 312c88a42738b272a2c13a387d34f6acaad71da8..e9574e09b9f8eaa0fd4be7617e3ae2ddadeab5bd 100644 (file)
@@ -22,6 +22,7 @@ TESTS = \
        basic-types/arrays.vala \
        basic-types/pointers.vala \
        basic-types/sizeof.vala \
+       basic-types/garray.vala \
        basic-types/glists.vala \
        basic-types/bug571486.vala \
        basic-types/bug591552.vala \
diff --git a/tests/basic-types/garray.vala b/tests/basic-types/garray.vala
new file mode 100644 (file)
index 0000000..055ba6a
--- /dev/null
@@ -0,0 +1,24 @@
+class Foo : Object {
+}
+
+void main () {
+       var array = new GLib.Array<Foo> ();
+
+       var foo = new Foo ();
+       assert (foo.ref_count == 1);
+
+       array.append_val (foo);
+       assert (foo.ref_count == 2);
+       array.remove_index (0);
+       assert (foo.ref_count == 1);
+
+       array.append_val (foo);
+       assert (foo.ref_count == 2);
+       array.remove_index_fast (0);
+       assert (foo.ref_count == 1);
+
+       array.append_val (foo);
+       assert (foo.ref_count == 2);
+       array.remove_range (0, 1);
+       assert (foo.ref_count == 1);
+}
index c8c5fa2ea44480e191c79897efda7e1e30349dcd..147a7148dc556a5056cbecbf06acbab1c1971c02 100644 (file)
@@ -5224,9 +5224,36 @@ namespace GLib {
                public void prepend_vals (void* data, uint len);
                public void insert_val (uint index, owned G value);
                public void insert_vals (uint index, void* data, uint len);
-               public void remove_index (uint index);
-               public void remove_index_fast (uint index);
-               public void remove_range (uint index, uint length);
+               [CCode (cname = "g_array_remove_index")]
+               public void _remove_index (uint index);
+               [CCode (cname = "g_array_remove_index_fast")]
+               public void _remove_index_fast (uint index);
+               [CCode (cname = "g_array_remove_range")]
+               public void _remove_range (uint index, uint length);
+               [CCode (cname = "vala_g_array_remove_index")]
+               public G remove_index (uint index) {
+                       assert (length > index);
+                       G g = data[index];
+                       _remove_index (index);
+                       return g;
+               }
+               [CCode (cname = "vala_g_array_remove_index_fast")]
+               public G remove_index_fast (uint index) {
+                       assert (length > index);
+                       G g = data[index];
+                       _remove_index_fast (index);
+                       return g;
+               }
+               [CCode (cname = "vala_g_array_remove_range")]
+               public G[] remove_range (uint index, uint length) {
+                       assert (this.length >= index + length);
+                       G[] ga = new G[length];
+                       for (uint i = 0; i < length; i++) {
+                               ga[i] = data[i + index];
+                       }
+                       _remove_range (index, length);
+                       return ga;
+               }
                public void sort (CompareFunc<G> compare_func);
                public void sort_with_data (CompareDataFunc<G> compare_func);
                [CCode (generic_type_pos = 0.1)]