GNOME Bugzilla – Bug 741466
SimpleType structs generate wrong C code
Last modified: 2017-03-09 21:37:06 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.
*** Bug 683686 has been marked as a duplicate of this bug. ***
https://bugzilla.gnome.org/show_bug.cgi?id=741467#c1
*** Bug 741467 has been marked as a duplicate of this bug. ***
*** This bug has been marked as a duplicate of bug 610083 ***