This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Global variable in static library - double free or corruption error

Hi Jeffrey,

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.


-----Original Message-----
From: Jeffrey Walton [] 
Sent: Thursday, October 21, 2010 5:00 PM
To: Alexey Skidanov
Subject: Re: Global variable in static library - double free or
corruption error

Hi Alexey,

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 [3]. [2] was
a test case to reproduce and looks exactly like your analysis :).

Anyway, the fix is to hide the the global TestClass:

TestClass test_var;

TestClass& GetTestClass()
    static TestClass test_var;
    return test_var;

I just opened a feature request for a -Wglobal-variable  for this sort
of thing:

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


[2] RTLD_GLOBAL and crash,
[3] Errors with multiple loading cryptopp as shared lib on Linux,

On Thu, Oct 21, 2010 at 7:53 AM, Alexey Skidanov
<> wrote:
> Hello,
> 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:
> [SNIP]
> 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?
> Thanks,
> Alexey

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]