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
runtime/cgo: call C exit to allow global destructors/atexit to run #20713
Comments
Could you clarify your CPP and Go code. |
//library files: #ifdef __cplusplus void tmp_log(const char* msg); #ifdef __cplusplus #endif //tmp_log.cpp extern "C" void tmp_log(const char* msg) } struct Auto_Init //compile //go files: go/src/tmp/log.go import( /* func Log(msg string) { //tool.go import ( func main() { //run tool: //c test, tool.c int main(int argc,char* argv[]) |
It's true that Go programs don't currently run the C++ static destructors of any code linked in via cgo. I guess the way to fix this would be for cgo programs to call the C library exit function rather than the exit system call. |
I agree we need to do this, although I am very sad about it. It didn't happen for Go 1.10, though, so at least there's that to be happy about. |
It occurs to me that this will have an unfortunate interaction with However, I don't think that interaction is fixable for Go 1, since the |
Just wanted to comment on other issues we have encountered due to this issue. When attempting to extract GCOV or mem track data for the C code, these tools require a C exit. Currently we have to explicitly call the exit() function in our test framework which prevents the go test code from finishing up. Please consider this when deciding the priority for the fix. We are looking forward to removing these workarounds once you have a fix. Thanks! |
Just a note that currently when building with |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.8 linux/amd64
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build855140157=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
What did you do?
I want to do something in the destructor of a global variable defined in c++ library which is linked into a go program via cgo. but the destructor can't be invoked when the go program exited.
//c++ library
class AutoInit
{
public:
AutoInit() { printf("+++++ auto init\n"); }
~AutoInit() { printf("---- auto fini\n"); }
};
What did you expect to see?
two lines should be printed:
+++++ auto init
---- auto fini
What did you see instead?
only one line was be printed:
+++++ auto init
The text was updated successfully, but these errors were encountered: