Skip to content

runtime: callback memory error #61147

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

Closed
mainjzb opened this issue Jul 3, 2023 · 5 comments
Closed

runtime: callback memory error #61147

mainjzb opened this issue Jul 3, 2023 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge OS-Windows

Comments

@mainjzb
Copy link

mainjzb commented Jul 3, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.5 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\mainj\AppData\Local\go-build
set GOENV=C:\Users\mainj\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\mainj\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\mainj\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.5
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\mainj\AppData\Local\Temp\go-build1190511740=/tmp/go-build -gno-record-gcc-switches

What did you do?

I want call C++ dll that has C API , the one API has callback function as parameter.
when I alloc memory in callback funtion , my go program will exit.

func MyCallBack() uintptr {
	b := make([]byte, 1024*1024)
	_ = b

	printMemUsage()
	return 0
}

func printMemUsage() {
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	fmt.Printf("Alloc = %v MiB\n", m.Alloc/1024/1024)
}
func main(){
    dll := MustLoadDLL("xx.dll")
    t := syscall.NewCallback(MyCallBack)
    cFunc = nfapi.MustFindProc("cFunc")

    type EH struct {
        callback uintptr
    }
    eh := EH{t}

    cFunc.Call(uintptr(unsafe.Pointer(&_eh)))
}

What did you expect to see?

The program should not exit.

What did you see instead?

the output :

Alloc = 1 MiB
Alloc = 2 MiB
Alloc = 3 MiB
Alloc = 4 MiB
Alloc = 1 MiB
Alloc = 2 MiB
Alloc = 3 MiB
Alloc = 4 MiB
Alloc = 1 MiB
Alloc = 2 MiB
Alloc = 3 MiB
Alloc = 4 MiB

exit status 0xc0000005

I see the Task Manager the process memroy increate to 8MB , process will exit in any way .

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 3, 2023
@mainjzb mainjzb changed the title runtime: callback memory error runtime: 1.20.5 callback memory error Jul 3, 2023
@ianlancetaylor
Copy link
Member

Can you show us a complete standalone program that demonstrates the problem?

@mainjzb
Copy link
Author

mainjzb commented Jul 3, 2023

https://github.com/mainjzb/go1.20.5_error
I upload my mini case.

@mainjzb mainjzb changed the title runtime: 1.20.5 callback memory error runtime: callback memory error Jul 4, 2023
@mainjzb
Copy link
Author

mainjzb commented Jul 4, 2023

#59724 Perhaps it's a related issue
I try these go version all will exit with 0xc0000005
1.18.10
1.19.10
1.20.5

It's seem about GC break stack and heap
The larger the created memory, the faster it crashes

@mainjzb
Copy link
Author

mainjzb commented Jul 6, 2023

debug.SetGCPercent(-1)
I add the code in func main to stop gc, It's will work fine.

@mainjzb
Copy link
Author

mainjzb commented Jul 6, 2023

I finally discovered the problem. The problem was that nf_EventHandler no escape . When the garbage collection (GC) occurred, it was being collected by the GC. Therefore, I redeclared a global variable, and the problem was resolved.

@golang golang locked and limited conversation to collaborators Jul 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge OS-Windows
Projects
None yet
Development

No branches or pull requests

4 participants