-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/cgo: Go-owned value zeroed after C call returns #27656
Comments
gcc version if helpful, |
cgo-exported functions are not allowed to return pointers to Go memory. (The Go garbage collector does not see the C stack or heap.) |
@bcmills I don't think the problem is related to |
res := make([]uint8, 2) allocates a slice in Go memory. return hdr.Data returns a Go-owned pointer as a |
The relevant part of the
|
Source which you have pointed out means these types contain Go pointers.
|
@bcmills consider re-opening the issue. |
But the pointer which I'm passing doesn't contain any other Go pointer, which means I can pass it to C. |
The rule, which @bcmills quoted above is: "A Go function called by C code may not return a Go pointer." You are looking at the rule for pointers that may be passed from Go code when calling C code. That is not what your program is doing. Your program is C code calling a Go function. That rule for that case is that the Go function may not return a Go pointer. It's not OK for an exported Go function to return a Go pointer that does not point to other Go pointers; an exported Go function may not return a Go pointer at all. |
@ianlancetaylor You cleared the point, but
If a Go function which is called by C code can take a Go pointer as argument, then how did the C program get the Go pointer in first place. |
The C function gets the Go pointer by itself being called by a Go function. (If you have a Go→C→Go call chain, the values passed in the Go→C call are pinned for the duration and therefore available for the C→Go call, since the C→Go call must return before the Go→C call can return.) |
@bcmills thanks a lot for clearing up. |
What version of Go are you using (
go version
)?go version go1.11 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOOS="linux"
Linux nil 4.18.6-arch1-1-ARCH #1 SMP PREEMPT GNU/Linux
What did you do?
go build -buildmode=c-shared -o libtemp.so; cp *.h *.so ../
gcc main.c -ltemp -L. -Wl,-R. ; ./a.out
What did you expect to see?
What did you see instead?
un-commenting
time.Sleep(time.Millisecond)
, gives correct result.The text was updated successfully, but these errors were encountered: