From: Rico Tzschichholz Date: Tue, 31 Mar 2020 08:39:38 +0000 (+0200) Subject: codegen: Don't free value if property setter takes ownership X-Git-Tag: 0.46.8~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9364ae3e406f34e38e33f2fae076abc733c0de14;p=thirdparty%2Fvala.git 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 --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index f53b1e93e..189b0fcde 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -5145,7 +5145,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 b6f831455..313a43808 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -364,6 +364,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); +}