null 'this' in constructor
Submitted by Vitaly Kirsanov
Link to original bug (#739995)
Description
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:
- _vala_assert() is made before 'self' has been allocated
- 'self' is set not to a Bar object but casted from a new Foo object.
Edited by Corentin Noël