GNOME Bugzilla – Bug 739995
null 'this' in constructor
Last modified: 2018-05-22 15:19:38 UTC
I made a mistake in my program which exposed curious behaviour of valac. I get null 'this' pointer in a constructor. Please consider the following code snippet: public class Foo { int i; public Foo.from(int i) { this.i = i; } } public class Bar { Foo f; public Bar() { assert( this != null ); f.from(1); // This line causes the trouble. // It should have been f = new Foo.from(1); } } int main(string[] argv) { var b = new Bar(); return 0; } If I run the compiled program I get this output: ** ERROR:/net/tieto/home/vkirsan/sandbit/test.vala.c:305:bar_construct: assertion failed: (this != null) Abort The produced C code for the constructor of Bar looks like this: Bar* bar_construct (GType object_type) { Bar* self = NULL; Foo* _tmp0_ = NULL; _vala_assert (self != NULL, "this != null"); _tmp0_ = self->priv->f; self = (Bar*) foo_construct_from (object_type, 1); return self; } So 1) _vala_assert() is made before 'self' has been allocated 2) 'self' is set not to a Bar object but casted from a new Foo object.
It's because vala thinks that f.from(1) is the constructor of self :) Should be fixed by the work at bug #567269
With Vala master, we get the following error: % valac ./bug739995.vala ./bug739995.vala:18.9-18.17: error: use `new' operator to create new objects f.from(1); // This line causes the trouble. ^^^^^^^^^ Compilation failed: 1 error(s), 0 warning(s) Can we close this now or do we still depend on bug 567269?
-- 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/483.