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 604299 - Chained constructors: wrong initialization
Chained constructors: wrong initialization
Status: RESOLVED DUPLICATE of bug 567269
Product: vala
Classification: Core
Component: Code Generator
0.7.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2009-12-10 18:47 UTC by Henrik /KaarPoSoft
Modified: 2010-10-14 15:15 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Henrik /KaarPoSoft 2009-12-10 18:47:05 UTC
The following code works fine:
class Test {
	int x;
	Test() {}
	Test.with_x(int x) { this.x=x; this(); }
	static int main() { var test = new Test.with_x(2); return 0; }
}

However, the following code gives a segmentation fault:
class Test {
	int x; int y;
	Test() { y=3; }
	Test.with_x(int x) { this.x=x; this(); }
	static int main() { var test = new Test.with_x(2); return 0; }
}

The reason can be found in the C code:
static Test* test_construct_with_x (GType object_type, gint x) {
        Test* self;
        self->priv->x = x;
        self = (Test*) test_construct (object_type);
        return self;
}

So, "self" is accessed before it is initialized.

I believe that either:
(1) self should be initialized; then the .with_x constructor code executed; then the basic constructor code.
OR
(2) valac should emit an error message in this case
Comment 1 zarevucky.jiri 2009-12-10 19:09:10 UTC
I think there was a discussion about this problem some time ago.
What I don't quite understand is the fact that Vala, being derived from C#, doesn't use the same chaining syntax, even though it fits the way this is implemented much better.

As for the possibilities you are proposing, (1) is impossible in most cases.
For (2), valac must enforce the chaining to be the first thing to call, which equals to the C# chaining with a bit different syntax (thus my amusement over the difference).
Comment 2 Jürg Billeter 2010-10-14 15:15:30 UTC
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find.

*** This bug has been marked as a duplicate of bug 567269 ***