summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2020-03-31 10:39:38 +0200
committerRico Tzschichholz <ricotz@ubuntu.com>2020-03-31 10:39:38 +0200
commit21900b6705c51c50049bc230718026b9c06c4f79 (patch)
treec98db5fac73164bb183072385272c7095a6efeef
parent2ab3b67c9b0c166c7d57c6a962c9c74a2f5220b9 (diff)
downloadvala-21900b6705c51c50049bc230718026b9c06c4f79.tar.gz
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
-rw-r--r--codegen/valaccodebasemodule.vala2
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/objects/member-initializer-property-owned-setter.vala22
3 files changed, 24 insertions, 1 deletions
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);
+}