I tried to run a simple in a go script from python and I got a segmentation fault. Here is my code:
main.go
package main
import (
/*
typedef struct foo{
int a;
int b;
int c;
int d;
int e;
int f;
} foo;
*/
"C"
)
func main() {}
//export Foo
func Foo(t []int) C.foo {
return C.foo{}
}
main.py
# loading shared object
lib = cdll.LoadLibrary("main.so")
# go type
class GoSlice(Structure):
_fields_ = [("data", POINTER(c_void_p)), ("len", c_longlong), ("cap", c_longlong)]
lib.Foo.argtypes = [GoSlice]
lib.Foo.restype = c_void_p
t = GoSlice((c_void_p * 5)(1, 2, 3, 4, 5), 5, 5)
f = lib.Foo(t)
print(f)
With that code, I got
140362617287784
[1] 23067 segmentation fault python3 main.py
Now If I remove e
and f
from the main.go
I got
None
and no more segmentation fault.
Why the number of members in the struct does matter here?
[EDIT] Both are running at the same place, I run one command clear && go build -o main.so -buildmode=c-shared main.go && python3 main.py