From 21900b6705c51c50049bc230718026b9c06c4f79 Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Tue, 31 Mar 2020 10:39:38 +0200 Subject: codegen: Don't free value if property setter takes ownership Correctly handle owned property accessor in object initializer. In addition to c0e955db075d3d155782c167a0abb81e0dce5f59 See https://gitlab.gnome.org/GNOME/vala/issues/953 --- codegen/valaccodebasemodule.vala | 2 +- tests/Makefile.am | 1 + .../member-initializer-property-owned-setter.vala | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/objects/member-initializer-property-owned-setter.vala diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 536aca2f6..db3c4f4e0 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -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)); } } diff --git a/tests/Makefile.am b/tests/Makefile.am index 882971a9b..c0018fdf7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 000000000..8f65b7b3d --- /dev/null +++ b/tests/objects/member-initializer-property-owned-setter.vala @@ -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); +} -- cgit v1.2.1