[vala/staging: 2/5] vala: No-accessor struct properties in GLib.Object class must be owned



commit d25f7f16c0af9b20332f4b5551b5760aaa759eb8
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu May 9 10:17:36 2019 +0200

    vala: No-accessor struct properties in GLib.Object class must be owned
    
    Real structs are returned as heap-allocated structor by g_object_get() and
    not defining them with owned getter results in memory leaks.
    
    Additionally force DelegateType, PointerType and ValueType as unowned to
    preserve the current behaviour for those.

 tests/Makefile.am                                  |  1 +
 .../objects/property-real-struct-no-accessor.test  | 13 ++++++++++++
 vala/valapropertyaccessor.vala                     | 23 ++++++++++++++++++++++
 3 files changed, 37 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 4527506aa..57e784b61 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -317,6 +317,7 @@ TESTS = \
        objects/property-construct-only-write.test \
        objects/property-construct-only-write-foreign.test \
        objects/property-gboxed-nullable.vala \
+       objects/property-real-struct-no-accessor.test \
        objects/property-static.vala \
        objects/regex.vala \
        objects/signals.vala \
diff --git a/tests/objects/property-real-struct-no-accessor.test 
b/tests/objects/property-real-struct-no-accessor.test
new file mode 100644
index 000000000..c2c956baf
--- /dev/null
+++ b/tests/objects/property-real-struct-no-accessor.test
@@ -0,0 +1,13 @@
+Invalid Code
+
+struct Foo {
+       public int i;
+}
+
+class Bar : Object {
+       [NoAccessorMethod]
+       public Foo foo { get; set; }
+}
+
+void main () {
+}
diff --git a/vala/valapropertyaccessor.vala b/vala/valapropertyaccessor.vala
index f9fc98171..37b0bf19e 100644
--- a/vala/valapropertyaccessor.vala
+++ b/vala/valapropertyaccessor.vala
@@ -158,6 +158,29 @@ public class Vala.PropertyAccessor : Subroutine {
                        value_parameter = new Parameter ("value", value_type, source_reference);
                }
 
+               if (readable && ((TypeSymbol) prop.parent_symbol).is_subtype_of 
(context.analyzer.object_type)) {
+                       //FIXME Code duplication with CCodeMemberAccessModule.visit_member_access()
+                       if (prop.get_attribute ("NoAccessorMethod") != null) {
+                               // g_object_get always returns owned values
+                               // therefore, property getters of properties
+                               // without accessor methods need to be marked as owned
+                               if (value_type.is_real_struct_type ()) {
+                                       if (source_reference == null || source_reference.file == null || 
source_reference.file.filename.has_suffix (".gir")) {
+                                               // Fix this silently for imported GIRs
+                                               value_type.value_owned = true;
+                                       } else if (!value_type.value_owned) {
+                                               Report.error (source_reference, "unowned return value for 
getter of property `%s' not supported without accessor".printf (prop.get_full_name ()));
+                                       }
+                               } else if (value_type.value_owned && value_type is DelegateType) {
+                                       value_type.value_owned = false;
+                               } else if (value_type.value_owned && value_type is PointerType) {
+                                       value_type.value_owned = false;
+                               } else if (value_type.value_owned && value_type is ValueType && 
!value_type.nullable) {
+                                       value_type.value_owned = false;
+                               }
+                       }
+               }
+
                if (prop.source_type == SourceFileType.SOURCE) {
                        if (body == null && !prop.interface_only && !prop.is_abstract) {
                                /* no accessor body specified, insert default body */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]