Golang c结构数组的可用分配内存

Golang c结构数组的可用分配内存,c,go,cgo,C,Go,Cgo,我正在寻找一种方法来释放Go中分配的c结构的内存。我试图使用C.free释放内存,但它抛出了对象0xc00024dfb0的malloc:**错误:未分配要释放的指针error package main /* #include "stdlib.h" #include "stdint.h" #include "stdio.h" typedef struct FileInfo{ char *Name;

我正在寻找一种方法来释放Go中分配的c结构的内存。我试图使用C.free释放内存,但它抛出了对象0xc00024dfb0的
malloc:**错误:未分配要释放的指针
error

package main

/*
#include "stdlib.h"
#include "stdint.h"
#include "stdio.h"
    typedef struct FileInfo{
        char    *Name;
        char    *Path;
    }FileInfo;

    typedef struct Result{
        FileInfo **files;
    }Result;
*/
import "C"
import (
    "unsafe"
)

func GetValue() {
    var aiList []*C.struct_FileInfo

    aif := &C.struct_FileInfo{}
    aif = (*C.struct_FileInfo)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_FileInfo{}))))

    aif.Name = C.CString("abcd")
    aif.Path = C.CString("/path")
    aiList = append(aiList, aif)

    air := &C.struct_Result{}
    air = (*C.struct_Result)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_Result{}))))
    air.files = &aiList[0]

    ptrAddr := *((*C.int64_t)(unsafe.Pointer(&air)))

    exportToFFI(ptrAddr)
}


func FreeMemory(pointer *int64, totalFiles int64){
    air := (*C.struct_Result)(unsafe.Pointer(pointer))
    files := (*[1 << 30]C.struct_FileInfo)(unsafe.Pointer(*air.files))[:totalFiles]

    for _, item := range files {

        C.free(unsafe.Pointer(&item.Name))
        C.free(unsafe.Pointer(&item.Path))
        C.free(unsafe.Pointer(&item))
    }
}
主程序包
/*
#包括“stdlib.h”
#包括“stdint.h”
#包括“stdio.h”
typedef结构文件信息{
字符*名称;
字符*路径;
}文件信息;
typedef结构结果{
文件信息**文件;
}结果;
*/
输入“C”
进口(
“不安全”
)
func GetValue(){
var aiList[]*C.struct_FileInfo
aif:=&C.struct_FileInfo{}
aif=(*C.struct_FileInfo)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_FileInfo{})))
aif.Name=C.CString(“abcd”)
aif.Path=C.CString(“/Path”)
aiList=附加(aiList,aif)
air:=&C.struct_结果{}
air=(*C.struct_Result)(C.malloc(C.size_t(unsafe.Sizeof(C.struct_Result{})))
air.files=&aiList[0]
ptrAddr:=*(*C.int64_t)(不安全的指针(&air)))
exportToFFI(ptrAddr)
}
func freemory(指针*int64,totalFiles int64){
空气:=(*C.struct_结果)(不安全的指针(指针))
文件:=(*[1