On Thu, Aug 17, 2017 at 03:32:28AM -0700, jianzhang...@gmail.com wrote:

> As the instruction 
> of https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices 
> shows, the GC of Go will not release it.
> An example as the following, but I have a question about when and how to 
> release this `slice`?

You shouldn't: the GC won't touch the memory it does not "own" so it
will eventually collect the slice structure itself but won't do anything
to its underlying array (which value you obtained via that double
type-conversion).

You should supposedly be instead concerned with freeing the array's
memory (via calling C.free or some dedicated function to do this
provided by your C side, if any).  As always with C, you should only do
that if that getTheArray() function's semantics are such that its caller
"owns" the memory the function returns.

> import "C"
> import "unsafe"
> ...
>         var theCArray *C.YourType = C.getTheArray()
>         length := C.getTheArrayLength()
>         slice := (*[1 << 
> 30]C.YourType)(unsafe.Pointer(theCArray))[:length:length]
> 
> 
> It is important to keep in mind that the Go garbage collector will not 
> > interact with this data, and that if it is freed from the C side of things, 
> > the behavior of any Go code using the slice is nondeterministic.

-- 
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