After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 739995 - null 'this' in constructor
null 'this' in constructor
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2014-11-12 08:13 UTC by Vitaly Kirsanov
Modified: 2018-05-22 15:19 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Vitaly Kirsanov 2014-11-12 08:13:17 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.
Comment 1 Luca Bruno 2015-03-13 09:41:17 UTC
It's because vala thinks that f.from(1) is the constructor of self :) Should be fixed by the work at bug #567269
Comment 2 Michael 'Mickey' Lauer 2018-02-25 14:18:22 UTC
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?
Comment 3 GNOME Infrastructure Team 2018-05-22 15:19:38 UTC
-- 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.