I am trying to implement a function in cgo that wraps a double
array in a Slice
:
//export ArrayToSlice
func ArrayToSlice(a *C.double, length int) (*[]float64) {
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(a)),
Len: length,
Cap: length,
}
return (*[]float64)(unsafe.Pointer(&hdr))
}
this function is supposed to be called from C, return the Slice to C, which will then pass it to another Go function.
This used to work with Go 1.5, but with 1.6 it started showing this panic:
panic: runtime error: cgo result has Go pointer
I understand what the issue is: Go does not want to hand out its pointers to C, because Go is garbage collected and might destroy pointers that are in the hands of the non-garbage collected C code.
Is there a way to do this in Go 1.6?