GNOME Bugzilla – Bug 734013
Object-type construct properties overwrite their default with null
Last modified: 2018-05-22 15:13:47 UTC
Created attachment 282115 [details] An example for an overwritten default value The attached file shows a class that has a construct-only property of type Object. Whenever the construct block of the property is called, it just outputs its value. By default the property shall be assigned some newly created Object. Running the program first outputs some address, indicating that the default indeed was set. But after that it outputs "(nil)" indicating that the property was again assigned, now to a null value. Would the property be stored, as an automatic property would do, this would overwrite it. This also happens with "set construct" blocks, but not with "set" blocks. They are called just once, as it should be expected from construct blocks also.
Created attachment 282116 [details] An example how it also happens with automatic properties
Created attachment 324683 [details] [review] patch proposal I found the root of the problem. Construct properties are set in the instance initializer function. This function is called during the GObject creation routine in the curse of g_object_new. After this was done, g_object_new looks at its arguments. Among these, no value is passed for the same construct property. That causes the property to be initialized again to null lacking any passed value. So the solution is to initialize construct properties as constructor arguments. To not run their initializer code twice, it has to be removed from the instance initialisation. Properties that only have a set accessor are not automatically initialised when not passed as arguments, so no change to their treatment is necessary. I tried to compile a patch to implement this behaviour. I tested it against the examples from this thread, "make check" and some of my private projects, but still would appreciate review as I am not sure whether I understood if properties are indeed expected to work this way.
Created attachment 339398 [details] Better test case The proposed patch still fails with this test code.
*** Bug 773661 has been marked as a duplicate of this bug. ***
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/463.