垃圾回收和cgo

11

在Go语言中,能否让垃圾回收器处理和释放通过C代码分配的内存?我很抱歉,我以前没有使用过C和cgo,所以我的示例可能需要一些解释。

假设你有一个想要使用的C库,并且该库分配了一些需要手动释放的内存。我想做的是像这样:

package stuff

/*
#include <stuff.h>
*/
import "C"

type Stuff C.Stuff

func NewStuff() *Stuff {
    stuff := Stuff(C.NewStuff()) // Allocate memory

    // define the release function for the runtime to call
    // when this object has no references to it (to release memory)   
    // In this case it's stuff.Free()     

    return stuff

}

func (s Stuff) Free() {
    C.Free(C.Stuff(s)) // Release memory
}
有没有办法让垃圾回收器在Go运行时中没有对*Stuff的引用时调用Stuff.Free()?我表达清楚了吗?也许更直接的问题是:是否可以编写一个函数,当该对象的引用计数为零时,运行时自动处理C分配的内存清理?
1个回答

14

存在runtime.SetFinalizer函数,但它不能用于由C代码分配的任何对象。

但是,您可以为每个需要自动释放的C对象创建一个Go对象:

type Stuff struct {
    cStuff *C.Stuff
}

func NewStuff() *Stuff {
    s := &Stuff{C.NewStuff()}
    runtime.SetFinalizer(s, (*Stuff).Free)
    return s
}

func (s *Stuff) Free() {
    C.Free(s.cStuff)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接