Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgo : free pointer can still use #43609

Closed
dreamerjackson opened this issue Jan 9, 2021 · 2 comments
Closed

cgo : free pointer can still use #43609

dreamerjackson opened this issue Jan 9, 2021 · 2 comments

Comments

@dreamerjackson
Copy link
Contributor

dreamerjackson commented Jan 9, 2021

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!

@dreamerjackson dreamerjackson changed the title cgo free pointer can still use cgo : free pointer can still use Jan 9, 2021
@davecheney
Copy link
Contributor

Thank you for raising this issue. I do not believe this is a bug in Go, this is just the way C works. However, it should be noted that as you've freed that memory you are into the area of underfined behaviour, after which, there are no guarentees.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@dreamerjackson
Copy link
Contributor Author

dreamerjackson commented Jan 9, 2021

@davecheney thanks, i just report a more strange cgo example in Stack Overflow ,can you look it ?
https://stackoverflow.com/questions/65642093/cgo-free-pointer-can-still-use

@golang golang locked and limited conversation to collaborators Jan 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants