对这个cgo代码恐慌的原因感到困惑

时间:2016-05-23 06:46:57

标签: go cgo

我想写一个关于使用golang call c函数的演示,我写这个文件,发现它会引起恐慌,我不知道为什么。

首先,c头文件p.h

    void output(char* str, int s);
    void cc(char *str);

其次,c文件p.c

    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>

    void output(char* str, int s) {
            fflush(stdout);
            sleep(s);
            printf("%s", str);
    }

    void cc(char *str) {
            printf("%s", c_ccB(str));
    }

非常简单,go代码为output函数,go代码为cc函数调用c_ccB,最后一个代码文件为p.go

     package main

     /*
     #include<stdlib.h>
     #include "p.h"
     */
     import "C"
     import "unsafe"
     import "fmt"

     //export c_ccB
     func c_ccB(cs *C.char) *C.char {
             gs := C.GoString(cs)
             return C.CString(gs + "wwww")
     }

     func main() { 
             ch := make(chan int)
             task("A", ch, 5)
             task("B", ch, 1)
             fmt.Printf("begin\n")
             <-ch
     }

     func task(name string, ch chan int, s int) {
             go func() {
                     i:= 1
                     for {
                              str := ":" + name
                              cstr := C.CString(str)
                              C.output(cstr, C.int(s))
                              C.cc(cstr)
                              C.free(unsafe.Pointer(cstr))
                              i++
                     }
                     ch <- 1
              }();
      }

go代码只是两个goroutime保持打印一些字符串,如果删除C.cc(cstr),它会正常工作,但为什么C.cc会导致恐慌?它只需在go中调用func c_ccB

这是恐慌信息:

      begin
      :B:Bwwww:B:Bwwww:B:Bwwww:B:Bwwwwfatal error: unexpected signal       during runtime execution
      [signal 0xb code=0x1 addr=0x900008e0 pc=0x7ff5973f8c80]

      runtime stack:
      runtime.throw(0x5438a0, 0x2a)
          /usr/local/go/src/runtime/panic.go:547 +0x90
      runtime.sigpanic()
          /usr/local/go/src/runtime/sigpanic_unix.go:12 +0x5a

      goroutine 5 [syscall, locked to thread]:
      runtime.cgocall(0x4b1b20, 0xc82002bf20, 0x0)
        /usr/local/go/src/runtime/cgocall.go:123 +0x11b                     fp=0xc82002bee8 sp=0xc82002beb8
      main._Cfunc_cc(0x7ff5900008c0)
        _/home/suwey/code/go/src/_obj/_cgo_gotypes.go:62 +0x36 fp=0xc82002bf20 sp=0xc82002bee8
      main.task.func1(0x51bf48, 0x1, 0x5, 0xc820062060)
        /home/suwey/code/go/src/p.go:32 +0xae fp=0xc82002bfa0 sp=0xc82002bf20
      runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1998 +0x1 fp=0xc82002bfa8 sp=0xc82002bfa0
      created by main.task
        /home/suwey/code/go/src/p.go:37 +0x53

      goroutine 1 [chan receive]:
      main.main()
        /home/suwey/code/go/src/p.go:22 +0xda

      goroutine 17 [syscall, locked to thread]:
      runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1998 +0x1

      goroutine 6 [syscall, locked to thread]:
      main._Cfunc_output(0x1de38b0, 0x1)
        _/home/suwey/code/go/src/_obj/_cgo_gotypes.go:74 +0x3a
      main.task.func1(0x51bfd0, 0x1, 0x1, 0xc820062060)
        /home/suwey/code/go/src/p.go:31 +0xa0
      created by main.task
        /home/suwey/code/go/src/p.go:37 +0x53

1 个答案:

答案 0 :(得分:0)

您错过了c代码中c_ccB的声明。请extern char* c_ccB(char*);向您p.c添加CString

而且,您忘记释放c_ccB中为function display_user_roles(){ $user_id = get_current_user_id(); $user_info = get_userdata( $user_id ); $user_roles = implode(', ', $user_info->roles); return $user_roles; } 分配的内存。