Skip to content

runtime: Will the variable pointer in Go side be invalid during calling C function due to garbage collection? #11345

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

Closed
shinningstar opened this issue Jun 23, 2015 · 3 comments
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge

Comments

@shinningstar
Copy link

Go garbage collector does not know Go variable(pointer) used in C world. In Go package source code:
https://github.com/golang/go/blob/master/src/net/cgo_unix.go#L47 here is a variable "hints" in Go world which is passed into C function getaddrinfo(). Theoretically, is it possible that the address of "hints" is invalid due to Go garbage collection?

@mikioh mikioh changed the title Will the variable pointer in Go side be invalid during calling C function due to garbage collection? runtime: Will the variable pointer in Go side be invalid during calling C function due to garbage collection? Jun 23, 2015
@mikioh mikioh added the Documentation Issues describing a change to documentation. label Jun 23, 2015
@ianlancetaylor
Copy link
Contributor

Pointers passed to C by cgo are live until the end of the cgo call. I don't think this needs to be documented as such, at least not until we actually resolve #8310.

The general cgo problem was raised in issue #10303 and fixed in http://golang.org/cl/10814.

@shinningstar
Copy link
Author

@ianlancetaylor From the discussion of #10303 , it seems that the pointer passed to C function does not escape until the fix in http://golang.org/cl/10814. So the problem I mentioned may be a real problem in current released 1.4.1/1.4.2. Is that true?

@ianlancetaylor
Copy link
Contributor

The 1.4.x and earlier releases are fine. In those releases, cgo generated C code, and pointers passed to C code always escape. Issue #10303 was reported against tip, after cgo had been changed to generate Go code. The fix was to cause the compiler to treat pointers as always escaping, which is exactly what it did in 1.4 and earlier releases.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants