GNOME Bugzilla – Bug 703996
Vala assumes that structs can be copied or destructor can be called on uninitialized struct
Last modified: 2014-06-03 18:34:31 UTC
For example the following code: [Compact] public class Node<K, V> { public WeakRef x = WeakRef(null); } results in following code: static void node_instance_init (Node * self) { GWeakRef _tmp0_ = {0}; g_weak_ref_init (&_tmp0_, NULL); self->x = _tmp0_; } Which is illegal as weak references need to know exact location of the struct. Moving initialization to constructor helps to some extend: Node* node_new (void) { Node* self; self = g_slice_new0 (Node); node_instance_init (self); g_weak_ref_clear (&self->x); // We destruct an object which wasn't constructed g_weak_ref_init (&self->x, NULL); return self; } static void node_instance_init (Node * self) { }
What about [CCode (lvalue_access = false)] on top of WeakRef?
No change with Vala from master
Right for instance variable initializer, however if you do that from a constructor it works.
The code's identical so it still clears the variable
The code I get in the init is: g_weak_ref_clear (&self->x); g_weak_ref_init (&self->x, NULL); And in fini: g_weak_ref_clear (&self->x); Isn't that what you expect?
(In reply to comment #5) > The code I get in the init is: > > g_weak_ref_clear (&self->x); > g_weak_ref_init (&self->x, NULL); > > And in fini: > > g_weak_ref_clear (&self->x); > > Isn't that what you expect? No - the constructor clears the struct which wasn't constructed, which probably should not happen.
Why not? It shouldn't cause any problem.
Do we have a test case that fails deterministically so I can add a regression test?
Regarding this code: > The code I get in the init is: > > g_weak_ref_clear (&self->x); > g_weak_ref_init (&self->x, NULL); The GObject reference manual at https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#g-weak-ref-clear states concerning g_weak_ref_clear "You should only call this on a GWeakRef that previously had g_weak_ref_init() called on it."
Ok then, the field initialization in classes have to take in account lvalue_access = false.
commit 02c7a414e00dc8e79c4cb9ad6c0c436df26c8dd5 Author: Luca Bruno <luca.bruno@immobiliare.it> Date: Tue Jun 3 11:57:07 2014 +0200 codegen: Simplify field initialization for struct types Fixes bug 703996 This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.