在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分配的内存清理?