On Wed, Mar 20, 2019 at 10:56 PM Cholerae Hu <cholerae...@gmail.com> wrote:
>
> package main
>
> // void a(long *p) {}
> import "C"
>
> import (
> "unsafe"
> )
>
> //go:noinline
> func b() {
> x := int64(0)
> C.a((*C.long)(unsafe.Pointer(&x)))
> // do something without x
> }
>
> func main() {
> b()
> }
>
> x should be considered unreachable after C.a . If x is something allocated on 
> heap, do I need to use runtime.KeepAlive(x) to make sure x will not be 
> garbage collected?

It's not necessary to use runtime.KeepAlive to keep pointers passed to
cgo functions alive.  They will be live for at least the duration of
the cgo call.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to