-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Description
i study and write the cgo code, i free the pointer but i still can use it ,why?
my code
package main
// #include <stdio.h>
// #include <stdlib.h>
// void myFunc(char *msg) {
// printf("Hello %s,%p!\n", msg,msg);
// }
import "C"
import (
"fmt"
"unsafe"
)
func main() {
m := C.CString("world")
fmt.Printf("msg.msg:%p,%p\n",unsafe.Pointer(m),m)
C.free(unsafe.Pointer(m))
C.myFunc(m)
}
What version of Go are you using (go version
)?
$ go version go 1.14.7 mac and go 1.12 centos all have this problem
What operating system and processor architecture are you using (go env
)?
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jackson/Library/Caches/go-build"
GOENV="/Users/jackson/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY="github.com/dreamEducation"
GONOSUMDB="github.com/dreamEducation"
GOOS="darwin"
GOPATH="/Users/xxx/go"
GOPRIVATE="github.com/dreamEducation"
GOPROXY="direct"
GOROOT="/Users/xxx/go/go1.14.7"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/xxx/go/go1.14.7/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/g2/0l4g444904vbn8wxnrw0j_980000gn/T/go-build764948702=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you expect to see?
panic because use the free pointer
What did you see instead?
msg.msg:0x4205a40,0x4205a40
Hello world,0x4205a40!