]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Don't free unowned heap allocated struct
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 12 Apr 2021 07:21:48 +0000 (09:21 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 12 Apr 2021 08:17:12 +0000 (10:17 +0200)
Regression of 63551acaf0d83fac8b50904c2759c1098fbfaa71

codegen/valaccodebasemodule.vala
tests/structs/cast-struct-boxed.vala

index 7466d5cec54f90ae0f91e36b15c93f721528819c..985b005c7a0f88711da2d31376f971aa23cef19f 100644 (file)
@@ -5423,7 +5423,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        while (inner_expr is CastExpression) {
                                inner_expr = ((CastExpression) inner_expr).inner;
                        }
-                       if (!(inner_expr.symbol_reference is Variable || inner_expr is ElementAccess)) {
+                       if (inner_expr.value_type.value_owned
+                           && !(inner_expr.symbol_reference is Variable || inner_expr is ElementAccess)) {
                                // heap allocated struct leaked, destroy it
                                var value = new GLibValue (new PointerType (new VoidType ()), innercexpr);
                                temp_ref_values.insert (0, value);
index 97ccd1d7d7917812ca926c68f6c5bc9173e4af87..86fff40645542f7ffaae9f283cad99b229d65709 100644 (file)
@@ -9,6 +9,11 @@ Foo? foo_heap_owned () {
        return foo;
 }
 
+unowned Foo? foo_heap_unowned () {
+       foo = { 42 };
+       return foo;
+}
+
 void test_without_destroy () {
        {
                Foo f = foo_heap_owned ();
@@ -22,6 +27,18 @@ void test_without_destroy () {
                Foo f = (!) foo_heap_owned ();
                assert (f.i == 23);
        }
+       {
+               Foo f = foo_heap_unowned ();
+               assert (f.i == 42);
+       }
+       {
+               Foo f = (Foo) foo_heap_unowned ();
+               assert (f.i == 42);
+       }
+       {
+               Foo f = (!) foo_heap_unowned ();
+               assert (f.i == 42);
+       }
 }
 
 struct Bar {
@@ -35,6 +52,11 @@ Bar? bar_heap_owned () {
        return bar;
 }
 
+unowned Bar? bar_heap_unowned () {
+       bar = { "manam" };
+       return bar;
+}
+
 void test_with_destroy () {
        {
                Bar b = bar_heap_owned ();
@@ -48,6 +70,30 @@ void test_with_destroy () {
                Bar b = (!) bar_heap_owned ();
                assert (b.s == "bar");
        }
+       {
+               Bar b = bar_heap_unowned ();
+               assert (b.s == "manam");
+       }
+       {
+               Bar b = (Bar) bar_heap_unowned ();
+               assert (b.s == "manam");
+       }
+       {
+               Bar b = (!) bar_heap_unowned ();
+               assert (b.s == "manam");
+       }
+       {
+               unowned Bar b = bar_heap_unowned ();
+               assert (b.s == "manam");
+       }
+       {
+               unowned Bar b = (Bar) bar_heap_unowned ();
+               assert (b.s == "manam");
+       }
+       {
+               unowned Bar b = (!) bar_heap_unowned ();
+               assert (b.s == "manam");
+       }
 }
 
 void main () {