英文:
Garbage collection and cgo
问题
在Go语言中,可以通过C代码分配和释放内存,但是垃圾回收器无法直接处理和释放通过C代码分配的内存。如果你想在Go语言中使用一个C库,并且这个库分配了一些需要手动释放的内存,你可以编写一个函数来释放这些内存。
在你的示例代码中,你定义了一个类型Stuff
,它是C.Stuff
的别名。你可以在NewStuff
函数中分配内存,并在Stuff
类型上定义一个Free
方法来释放内存。但是,垃圾回收器无法自动调用Stuff.Free
方法来释放内存。
所以,回答你的问题,垃圾回收器无法在Go运行时中没有对*Stuff
的引用时调用Stuff.Free
方法。你需要手动调用Stuff.Free
方法来释放内存。
希望这样回答对你有帮助。
英文:
Is it possible to make the garbage collector in Go handle and release memory allocated through C code? I apologize, I haven't used C and cgo before so my examples may need some clarification.
Lets say you've got some C library that you'd like to use and this library allocates some memory that needs to be freed manually. What I'd like to do is something like this:
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
}
Is there any way for the garbage collector to call Stuff.Free() when there are no references to *Stuff in the Go runtime?
Am I making sense here?
Perhaps a more direct question is: Is it possible to make the runtime automatically handle the cleanup of C allocated memory by writing a function that the runtime calls when there are zero references to that object?
答案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)
}
英文:
There exists the runtime.SetFinalizer
function, but it cannot be used on any object allocated by C code.
However, you can create a Go object for each C object that needs to be freed automatically:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论