summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2018-07-27 16:25:44 +0200
committerRico Tzschichholz <ricotz@ubuntu.com>2018-07-27 16:25:44 +0200
commita62bda136d404743305161326390f3db3880c9d5 (patch)
treee1bfd8f28de49f6e343044907af7a37c5bfe7fbe
parent64cca878abe575a583164f2ae42817200d3ecf0b (diff)
downloadvala-a62bda136d404743305161326390f3db3880c9d5.tar.gz
glib-2.0: Add Array.remove*() wrapper to avoid leaking generic elements
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/basic-types/garray.vala24
-rw-r--r--vapi/glib-2.0.vapi33
3 files changed, 55 insertions, 3 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 312c88a42..e9574e09b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..055ba6a2a
--- /dev/null
+++ b/tests/basic-types/garray.vala
@@ -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);
+}
diff --git a/vapi/glib-2.0.vapi b/vapi/glib-2.0.vapi
index c8c5fa2ea..147a7148d 100644
--- a/vapi/glib-2.0.vapi
+++ b/vapi/glib-2.0.vapi
@@ -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)]