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

Closed
nowind opened this issue Jun 8, 2019 · 8 comments
Closed

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

nowind opened this issue Jun 8, 2019 · 8 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@nowind
Copy link

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 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 NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows labels Jun 14, 2019
@FiloSottile FiloSottile added this to the Unplanned milestone Jun 14, 2019
@FiloSottile
Copy link
Contributor

/cc @ianlancetaylor

@mattn
Copy link
Member

mattn commented Aug 8, 2019

dup #11100

@Green7
Copy link

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.

@jlevitt
Copy link

jlevitt commented Apr 26, 2022

I'm also seeing this issue trying to use a golang dll I wrote in Visual Studio 2017.

@funte

This comment was marked as duplicate.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@BigCatGit

This comment was marked as spam.

@seankhliao
Copy link
Member

seankhliao commented Jan 2, 2023

Duplicate of #11100

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2023
@seankhliao seankhliao marked this as a duplicate of #11100 Jan 2, 2023
@golang golang locked and limited conversation to collaborators Jan 2, 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 NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

10 participants