登录
首页 >  Golang >  Go问答

混乱之源:带有 Go 指针的 cgo 运行时错误

来源:stackoverflow

时间:2024-03-05 11:33:28 155浏览 收藏

一分耕耘,一分收获!既然都打开这篇《混乱之源:带有 Go 指针的 cgo 运行时错误》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

你好,我最近试图从 python 代码调用一个 golang 函数,该函数使用 for 循环打印一些除法的内容。

recap.go 的完整代码:

package main

import (
    "c"
    "strconv"
)

//export printalg
func printalg(debug bool) string {
    var b200 int = 0
    var b300 int = 0
    var b400 int = 0
    var x string = "a"
    if debug == true {
        // extra
    } else if debug == false {
        for i := 1; i <= 100; i++ {
            if i%4 == 0 && i%3 == 0 {
                b200++
                b300++
                b400++
            } else if i%4 == 0 && !(i%3 == 0) {
                b400++
                b200++
            } else if i%2 == 0 && i%3 == 0 && !(i%4 == 0) {
                b200++
                b300++
            } else if i%3 == 0 && !(i%2 == 0) && !(i%4 == 0) {
                b300++
            } else if i%2 == 0 && !(i%3 == 0) && !(i%4 == 0) {
                b200++
            } else if i%2 == 0 && i%3 == 0 && i%4 == 0 {
                b200++
                b300++
                b400++
            }
        }
        x = strconv.itoa(b200) + " " + strconv.itoa(b300) + " " + strconv.itoa(b400)
    }
    return x
}

func main() {

}


然后我尝试使用 ctypes 调用 printalg 函数(在 python 代码中)。然后当我从cmd运行python代码时,发生了这个错误:

panic: runtime error: cgo result has Go pointer

goroutine 17 [running, locked to thread]:
panic({0x67322a80, 0xc00004a0a0})
        C:/Program Files/Go/src/runtime/panic.go:1147 +0x3a8
runtime.cgoCheckArg(0x67320da0, 0xc00004a090, 0x10, 0x0, {0x6732cc4f, 0x19})
        C:/Program Files/Go/src/runtime/cgocall.go:514 +0x4d8
runtime.cgoCheckResult({0x67320da0, 0xc00004a090})
        C:/Program Files/Go/src/runtime/cgocall.go:630 +0x54
_cgoexp_ba17693967af_PrintAlg(0x108efee9c0)
        _cgo_gotypes.go:39 +0x5a
runtime.cgocallbackg1(0x67315640, 0x0, 0x0)
        C:/Program Files/Go/src/runtime/cgocall.go:306 +0x29a
runtime.cgocallbackg(0x0, 0x0, 0x0)
        C:/Program Files/Go/src/runtime/cgocall.go:232 +0x106
runtime.cgocallbackg(0x67315640, 0x108efee9c0, 0x0)
        :1 +0x36
runtime.cgocallback(0x0, 0x0, 0x0)
        C:/Program Files/Go/src/runtime/asm_amd64.s:915 +0xd7
runtime.goexit()
        C:/Program Files/Go/src/runtime/asm_amd64.s:1581 +0x1

我可以做什么来修复这个错误?


正确答案


字符串的 go 运行时实现是

type stringstruct struct {
    str unsafe.pointer
    len int
}
panic: runtime error: cgo result has go pointer

阅读 cgo documentation,尤其是 Passing_pointers 了解详情。

在 go 中编写

func printalg(debug bool) *c.char {
    // ...
    return c.cstring(x)
}

返回指向 c 字符串实例的指针。不再需要时释放指针。

$ go build -buildmode=c-shared -o _recap.so
$ python3 recap.py
50 33 25
$

recap.go

package main

import (
    "c"
    "strconv"
)

//export printalg
func printalg(debug bool) *c.char {
    var b200 int = 0
    var b300 int = 0
    var b400 int = 0
    var x string = "a"
    if debug == true {
        // extra
    } else if debug == false {
        for i := 1; i <= 100; i++ {
            if i%4 == 0 && i%3 == 0 {
                b200++
                b300++
                b400++
            } else if i%4 == 0 && !(i%3 == 0) {
                b400++
                b200++
            } else if i%2 == 0 && i%3 == 0 && !(i%4 == 0) {
                b200++
                b300++
            } else if i%3 == 0 && !(i%2 == 0) && !(i%4 == 0) {
                b300++
            } else if i%2 == 0 && !(i%3 == 0) && !(i%4 == 0) {
                b200++
            } else if i%2 == 0 && i%3 == 0 && i%4 == 0 {
                b200++
                b300++
                b400++
            }
        }
        x = strconv.itoa(b200) + " " + strconv.itoa(b300) + " " + strconv.itoa(b400)
    }
    return c.cstring(x)
}

func main() {}

recap.py

import ctypes
recap = ctypes.cdll.LoadLibrary('./_recap.so')
printAlg = recap.PrintAlg
printAlg.argtypes = [ctypes.c_bool]
printAlg.restype = ctypes.c_void_p
free = recap.free
free.argtypes = [ctypes.c_void_p]
px = printAlg(False)
x = ctypes.string_at(px).decode('utf-8')
free(px)
print(x)

到这里,我们也就讲完了《混乱之源:带有 Go 指针的 cgo 运行时错误》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>