Golang字节数组到C#,.NetCore,如何释放非托管内存的不安全ptr
我现在从C#p/Invoke中的go with C.CBytes读取字节数组 C# 戈兰Golang字节数组到C#,.NetCore,如何释放非托管内存的不安全ptr,c#,go,C#,Go,我现在从C#p/Invoke中的go with C.CBytes读取字节数组 C# 戈兰 //export GetData func GetData() unsafe.Pointer { //read bytes to data, data is byte[] buf := new(bytes.Buffer) if data == nil { writeInt32(buf, int32(0)) }else { writeInt32(
//export GetData
func GetData() unsafe.Pointer {
//read bytes to data, data is byte[]
buf := new(bytes.Buffer)
if data == nil {
writeInt32(buf, int32(0))
}else {
writeInt32(buf, int32(len(data)))
binary.Write(buf, binary.LittleEndian, data)
}
cData := C.CBytes(buf.Bytes())
return cData
}
//export Free
func Free(ptr unsafe.Pointer){
if ptr != nil{
log.Println("Free c memory", ptr)
C.free(ptr)
}
}
我发现非托管内存没有释放,所以我在go中添加了一个Free func,并在C中调用它#
戈兰
//export GetData
func GetData() unsafe.Pointer {
//read bytes to data, data is byte[]
buf := new(bytes.Buffer)
if data == nil {
writeInt32(buf, int32(0))
}else {
writeInt32(buf, int32(len(data)))
binary.Write(buf, binary.LittleEndian, data)
}
cData := C.CBytes(buf.Bytes())
return cData
}
//export Free
func Free(ptr unsafe.Pointer){
if ptr != nil{
log.Println("Free c memory", ptr)
C.free(ptr)
}
}
C#
但结果是非托管内存仍然被占用
这里有什么问题?我用错了吗