duanliao6789 2018-09-09 16:27
浏览 144

Go GC是否会尝试在reflect.SliceHeader结构中从unsafe.Pointer中释放“外部堆”?

So I found out how to create a slice from an unsafe pointer in Go:

func cArray2ByteSlice(array unsafe.Pointer, len int) (list []byte) {
    sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&list)))
    sliceHeader.Cap = len
    sliceHeader.Len = len
    sliceHeader.Data = uintptr(array)
    return list
}

I have a window of opportunity to access the heap memory at array unsafe.Pointer between calling the external C API lock and unlock.

The GC will eventually attempt to collect the slice list []byte. The thing is: the slice header is in golang allocated heap, but the sliceHeader.Data is not.

Is the GC smart enough to ignore none-golang heap allocation in that situation, as in most others?

After calling unlock, I want to be sure that Go GC doesn't attempt to release the heap memeory at sliceHeader.Data.

Also if I want to copy the data for use outside of the lock/unlock period, I assume I could return this var instead:

 var safeList []byte = list[:]
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作