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

cmd/cgo: FreeLibrary a go c-shared dll make app crash #32497

Open
nowind opened this issue Jun 8, 2019 · 4 comments
Open

cmd/cgo: FreeLibrary a go c-shared dll make app crash #32497

nowind opened this issue Jun 8, 2019 · 4 comments

Comments

@nowind
Copy link

@nowind nowind commented Jun 8, 2019

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

$ go version
go version go1.12.5 windows/386

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 GOARCH=386
set GOBIN=
set GOCACHE=C:\Users\nowind\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=386
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\tools\go\user
set GOPROXY=https://goproxy.io
set GORACE=
set GOROOT=E:\tools\go386
set GOTMPDIR=
set GOTOOLDIR=E:\tools\go386\pkg\tool\windows_386
set GCCGO=gccgo
set GO386=sse2
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m32 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\now
ind\AppData\Local\Temp\go-build636168646=/tmp/go-build -gno-record-gcc-switches

What did you do?

dll.go:

package main

import (
	"C"
	"fmt"
)

//export SayHi
func SayHi() {
	fmt.Println("Hi, I'm go dll")
}

func main() {
}

dll.c

int main(int argc, char* argv[])
{
	HMODULE dll= ::LoadLibrary("dll.dll");
	if(dll==NULL)
	{
		printf("err!\n");
		return -1;
	}
       getchar();
	::FreeLibrary(dll);
        getchar();
	return 0;
}

What did you expect to see?

What did you see instead?

after call FreeLibrary ,app crash ,but no error messagebox

@beoran
Copy link

@beoran beoran commented Jun 12, 2019

You are not calling the Go SayHi function anywhere on the C side, that is probably why you don't see anything happen. You probably need to add something like void SayHi(void) = (GetProcAddress(dll, "SayHi")); SayHi(); before the FreeLibrary. Also, include Windows.h at the top of your C program to get the correct prototypes.

@FiloSottile FiloSottile changed the title FreeLibrary a go c-shared dll make app crash cmd/cgo: FreeLibrary a go c-shared dll make app crash Jun 14, 2019
@FiloSottile FiloSottile added this to the Unplanned milestone Jun 14, 2019
@FiloSottile
Copy link
Member

@FiloSottile FiloSottile commented Jun 14, 2019

@mattn
Copy link
Member

@mattn mattn commented Aug 8, 2019

dup #11100

@Green7
Copy link

@Green7 Green7 commented Nov 2, 2019

I have this same problem. Golang based dll is used in installer app (on windows). When installer finishes, it releases all used libraries. Occasionally this causes panic in golang code (from the released library). In this case process exit code is changed and program ends with 0xC0000005 (reported by Windows as Integer value, so for example in .bat file errorlevel shows -1073741819).
It's very confusing and it took me a long time to detect the problem because it doesn't happen every time. It's easy to repeat this error in c ++ code: all you nedd is call function from golang dll, next FreeLibrary and make short delay (Sleep(2000) is enough) before the program ends.
Workaround: as suggested in #11100, do not release the library. In my case I "lock" the library by an additional call to LoadLibrary from the library itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.