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 741466 - SimpleType structs generate wrong C code
SimpleType structs generate wrong C code
Status: RESOLVED DUPLICATE of bug 610083
Product: vala
Classification: Core
Component: Code Generator
0.35.x
Other All
: High normal
: 1.0
Assigned To: Vala maintainers
Vala maintainers
: 683686 741467 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2014-12-13 00:02 UTC by pancake
Modified: 2017-03-09 21:37 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description pancake 2014-12-13 00:02:05 UTC
The following code:
```
[SimpleType]
public struct MyTest {
	int foo;
	int bar;
	public MyTest() {
		print ("Works!\n");
	}
}

public void main() {
	MyTest ();
}
```

produces:

```
/Users/pancake/prg/posixvala/tests/iface.vala.c:28:10: error: passing 'MyTest' (aka 'struct _MyTest') to parameter of incompatible type 'const void *'
        memset (self, 0, sizeof (MyTest));
                ^~~~
/usr/include/secure/_string.h:77:59: note: expanded from macro 'memset'
  __builtin___memset_chk (dest, val, len, __darwin_obsz0 (dest))
                                                          ^
/usr/include/secure/_common.h:38:55: note: expanded from macro '__darwin_obsz0'
#define __darwin_obsz0(object) __builtin_object_size (object, 0)
                                                      ^
/Users/pancake/prg/posixvala/tests/iface.vala.c:59:25: error: too few arguments to function call, single argument 'self' was not specified
        _tmp0_ = my_test_init ();
                 ~~~~~~~~~~~~  ^
/Users/pancake/prg/posixvala/tests/iface.vala.c:27:1: note: 'my_test_init' declared here
void my_test_init (MyTest self) {
^
2 errors generated.
```

That's because the constructor is wrongly defined as follows:

```
void my_test_init (MyTest self) {
        memset (self, 0, sizeof (MyTest));
        g_print ("Works!\n");
}

void _vala_main (void) {
        MyTest _tmp0_ = {0};
        _tmp0_ = my_test_init ();
}
```

The magic of [SimpleType] makes the struct to be returned by functions in the return value instead of an out parameter (mytestinit(&_tmp0_)). So looks like Vala is not honoring that attribute in the constructor.

Also, there's a destructor which calls g_free() and i bet that will produce a runtime crash. SimpleTypes shouldnt be freed because they live in the stack.
Comment 1 Rico Tzschichholz 2017-03-09 21:21:58 UTC
*** Bug 683686 has been marked as a duplicate of this bug. ***
Comment 2 Rico Tzschichholz 2017-03-09 21:22:58 UTC
https://bugzilla.gnome.org/show_bug.cgi?id=741467#c1
Comment 3 Rico Tzschichholz 2017-03-09 21:23:13 UTC
*** Bug 741467 has been marked as a duplicate of this bug. ***
Comment 4 Rico Tzschichholz 2017-03-09 21:37:06 UTC

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