GNOME Bugzilla – Bug 597488
Initializing an ArrayList in the member declaration causes G_IS_OBJECT errors
Last modified: 2017-02-17 21:01:20 UTC
Vala 0.7.7 with libgee 0.5.0. If I run the attached code then I get the following error: GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed This happens when retrieving objects from the array. Frederik @ #vala remarked that moving the initializer from the class body to the constructor causes the errors to disappear.
Created attachment 144858 [details] Testcase showing problem and work-around
Created attachment 144859 [details] Backtrace from gdb
This happens because new ArrayList<T>() is done in the instance_init. Unfortunately, the generic type information is given after instance_init. Would passing generic type information as properties through g_object_new solve the problem or start a new era of bugs due to property overriding? Maybe we should simply forbid using initializers that refer type parameters?
duplicate of bug 597488
(In reply to comment #4) > duplicate of bug 597488 It's this bug :-)
Should have been: Bug 523767 - Generics don't work in field initializers however comment 4 describes the same problem.
W hen using Gee and Vala generics as properties, you should consider to initialize them first, both using a default or in construct block. public class MyClass : Object { public ArrayList<Object> { get; set: default = new ArrayList<Object> (); } } Generics types are unknown at class initialization, but at instantiation time, then you should instantiate it and know generic type first. This was done at GXml for SerializableArrayList<G>, where any object with this property type is initalized at declration using defaulto = new SerializableArrayList<Object>(); this avoids types unknowns. In GXml Serializable, added an interface to declare it is a Collection Container then after create an object's instance using Object.new(), it calls a method implemented in interface to initialize all its generic property members.