Thanks your reply, you mean I should be release the memory of `theCArray` 
in C side?
I still have some doubts, and I made a clearer example as the below, any 
problem with it?
So, the memory of this tmpslice should be release by release the argv on C 
side? 

func GoStrings(length int, argv **C.char) []string {
        
        tmpslice := (*[1 << 30]*C.char)(unsafe.Pointer(argv))[:length:length
]
        gostrings := make([]string, length)
        for i, s := range tmpslice {
                gostrings[i] = C.GoString(s)
                defer C.free(unsafe.Pointer(s))
        }
        return gostrings
}





在 2017年8月17日星期四 UTC+8下午8:41:48,Konstantin Khomoutov写道:
>
> On Thu, Aug 17, 2017 at 03:32:28AM -0700, jianzh...@gmail.com 
> <javascript:> 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