I want to write a demo about use golang call c function, and I write this files and found it will get panic and I don't know why.
First, a c header file p.h
:
void output(char* str, int s);
void cc(char *str);
Second, a c file 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));
}
It's quite simple, a output
function for go code and a cc
function call c_ccB
in go code, the last go code file is 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
}();
}
The go code is just two goroutime keep print some strings, if delete C.cc(cstr)
, it will work fine, but why C.cc
will cause panic ? It just call func c_ccB
in go.
Here is the panic message:
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
you missed the declaration of c_ccB
in your c code. add extern char* c_ccB(char*);
to you p.c
please.
And, you forget to free the memory allocated for CString
in c_ccB
.