]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Don't free value if property setter takes ownership
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 31 Mar 2020 08:39:38 +0000 (10:39 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 31 Mar 2020 08:39:38 +0000 (10:39 +0200)
Correctly handle owned property accessor in object initializer.

In addition to c0e955db075d3d155782c167a0abb81e0dce5f59

See https://gitlab.gnome.org/GNOME/vala/issues/953

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/objects/member-initializer-property-owned-setter.vala [new file with mode: 0644]

index 536aca2f611a7d685b6512c1f53e2bf166636bd8..db3c4f4e0907e76d9b2cee331be7c3391df269d3 100644 (file)
@@ -5075,7 +5075,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                        inst_ma.target_value = typed_inst;
                                        store_property (p, inst_ma, init.initializer.target_value);
                                        // FIXME Do not ref/copy in the first place
-                                       if (requires_destroy (init.initializer.target_value.value_type)) {
+                                       if (!p.set_accessor.value_type.value_owned && requires_destroy (init.initializer.target_value.value_type)) {
                                                ccode.add_expression (destroy_value (init.initializer.target_value));
                                        }
                                }
index 882971a9bef872c8319593609759302aa5b7f73e..c0018fdf7dc51d58256c9ee3eb6489f88a2c242e 100644 (file)
@@ -380,6 +380,7 @@ TESTS = \
        objects/interface-property-override.vala \
        objects/interface-virtual-override.vala \
        objects/member-initializer-base-properties.vala \
+       objects/member-initializer-property-owned-setter.vala \
        objects/methods.vala \
        objects/paramspec.vala \
        objects/plugin-module-init.vala \
diff --git a/tests/objects/member-initializer-property-owned-setter.vala b/tests/objects/member-initializer-property-owned-setter.vala
new file mode 100644 (file)
index 0000000..8f65b7b
--- /dev/null
@@ -0,0 +1,22 @@
+class Bar : Object {
+}
+
+class Foo : Object {
+       public string[] faz { get; owned set; }
+       public Bar bar { get; owned set; }
+}
+
+void main() {
+       string[] sa = { "foo", "bar" };
+       var o = new Bar ();
+
+       var foo = new Foo () {
+               faz = sa,
+               bar = o
+       };
+
+       assert (foo.faz[1] == "bar");
+       assert (foo.bar.ref_count == 2);
+       assert (sa[0] == "foo");
+       assert (o.ref_count == 2);
+}