runtime.SetFinalizer:无法确定 C.Char 类型的名称
时间:2024-03-12
浏览:110
作者:佚名
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《runtime.SetFinalizer:无法确定 C.Char 类型的名称》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
请考虑下面的示例代码:
package main
/*
#include <stdio.h>
#include <stdlib.h>
*/
import "c"
import (
"fmt"
"runtime"
"unsafe"
)
func main() {
// convert go string to c string using c.cstring
cstring := c.cstring("wold!")
fmt.printf("c.cstring type: %t\n", cstring)
//c.free(unsafe.pointer(cstring)) // <-- this works, but i don't want to free it manually..
runtime.setfinalizer(&cstring, func(t *c.char) {
c.free(unsafe.pointer(t))
})
}
我正在尝试使用cgo,并尝试释放cstring。当我尝试使用 runtime.setfinalizer 释放变量 cstring 时,我遇到:
$ go build a.go # command-line-arguments ./a.go:22:41: could not determine kind of name for C.Char
请给我指出正确的方向。谢谢!
解决方案
当 cgo 系统将你的包装器转换为 go 编译器可以理解的东西时,它必须将每个 c 类型转换为 go 类型以用于各种目的。事实证明这不适用于您的情况(这是您看到的错误)。
这实际上没问题,因为您的代码一开始就不会按照您想要的方式工作。当 go 的垃圾收集器准备释放占用 go 内存的 go 对象,但 c.cstring 返回一个不是 go 内存的指针时,运行时终结器就会运行。特别要注意以下来自 the cgo documentation 的引用:
// go string to c string // the c string is allocated in the c heap using malloc. // it is the caller's responsibility to arrange for it to be // freed, such as by calling c.free (be sure to include stdlib.h // if c.free is needed). func c.cstring(string) *c.char
由于返回的字符串位于“c 堆”上,因此 go 垃圾收集器永远不会最终确定它。如果你的代码被编译,它就只是一个空操作。
如果你有一个 go 对象,其生命周期与 c 对象的生命周期相似,你也许可以使用它。这是一个虚构的(但有效的)示例:
package main
/*
#include <stdio.h>
#include <stdlib.h>
*/
import "c"
import (
"fmt"
"runtime"
"time"
"unsafe"
)
type s struct {
foo int
tofree unsafe.pointer
}
func main() {
doit()
runtime.gc()
time.sleep(10 * time.millisecond) // ugly hack
}
func doit() {
cstring := c.cstring("wold!")
fmt.printf("c.cstring type: %t\n", cstring)
x := &s{foo: 1, tofree: unsafe.pointer(cstring)}
runtime.setfinalizer(x, func(t *s) {
fmt.println("freeing c string")
c.free(t.tofree)
})
}
当为 x 分配的对象超出范围时,它就符合 gc 的条件。实际的 gc 可能永远不会发生,所以我在 main 中使用 runtime.gc() 强制进行一次。这会触发终结器:
$ ./cfree_example C.CString type: *main._Ctype_char freeing C string
“丑陋的黑客”就在那里,因为如果 main 在终结器调用完成写入 freeing c string 消息之前返回,它就会丢失。在真实的程序中你不需要这个。
好了,本文到此结束,带大家了解了《runtime.SetFinalizer:无法确定 C.Char 类型的名称》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
