This is the mail archive of the
mailing list for the GCC project.
RE: Global variable in static library - double free or corruption error
- From: "Alexey Skidanov" <Alexey dot Skidanov at orbotech dot com>
- To: <noloader at gmail dot com>
- Cc: <gcc-help at gcc dot gnu dot org>, <skidanovalexey at yahoo dot com>
- Date: Thu, 21 Oct 2010 17:34:30 +0200
- Subject: RE: Global variable in static library - double free or corruption error
Thanks for your response. You are absolutely right regarding to
-Wglobal-variable. I would prefer to get some kind of error (linking
error?) about two global variables with the same name instead of dynamic
loader/linker decision to "merge" them.
From: Jeffrey Walton [mailto:email@example.com]
Sent: Thursday, October 21, 2010 5:00 PM
To: Alexey Skidanov
Cc: firstname.lastname@example.org; email@example.com
Subject: Re: Global variable in static library - double free or
I saw the same thing in Crypto++ when distributions began
porting/packaging as shared object [1, 2]. The mailing list was
getting 'double free' reports under obscure circumstances .  was
a test case to reproduce and looks exactly like your analysis :).
Anyway, the fix is to hide the the global TestClass:
static TestClass test_var;
I just opened a feature request for a -Wglobal-variable for this sort
of thing: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46097.
You might chime in on the issue since the GCC team is contemplating
whether the feature is even worth while. I claim it is because this
behavior has jumped up a bit a few people in the ass. I was thinking
that -Wglobal-variable should be a part of -Wextra, and possibly
 RTLD_GLOBAL and libcryptopp.so crash,
 Errors with multiple loading cryptopp as shared lib on Linux,
On Thu, Oct 21, 2010 at 7:53 AM, Alexey Skidanov
> We use the gcc 4.1.1 in our project. After some code refactoring
> (creating new shared library) the application crashes at exit with
> following error:
> *** glibc detected *** : double free or corruption (fasttop) ***
> In order to simulate our application, I created the test one, with two
> shared libraries and one static library. The code is below:
> Static library defines some global variable (test_var), that during
> library linking "injected" into the dynamic library bss segment.
> Actually, static.o object file is linked with dynamic1.o and with
> dynamic2.o files.
> Please, pay attention that constructor and destructor of the SAME
> INSTANCE (at address 0x60ca9c) of global variable were called twice -
> each dynamic library start up. Generally, it seems that this is
> solution of double definition problem during main application linking:
> Dynamic linker somehow merges two definition of the same global
> - but each shared library start up code contains calls of this
> constructor and destructor.
> I would expect that TWO different instances of the global variable
> be created in TWO different shared libraries - maybe with name
> Is this a linker/loader bug? Can we cause (by linker/loader options)
> linker/loader more consistent behavior?