This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Serious C++ linking error
- To: egcs-bugs at egcs dot cygnus dot com
- Subject: Serious C++ linking error
- From: Jason Gunthorpe <jgg at ualberta dot ca>
- Date: Sat, 3 Jul 1999 20:55:37 -0600 (MDT)
Hi,
Some number of months ago someone reported a problem with C++ strings
leaking memory when some parts of a program are compiled with -fPIC. Since
this has been bothering me for months and months I sat down and have
tracked this down to the real cause. I hope someone can fix it!
It appears that my eg++ is causing .o files linked with -fPIC to
dynamically link to template symbols located in libstdc++2.9, while my
other .o files link to a local copy of those template symbols. One of
these symbols is the free_list in the __default_alloc_template - the
default allocator for the string class.
Since there are two seperate free_list symbols what happens is that the
shared library allocates a new object, then gives that to the caller who
then releases it onto it's copy of the free_list, this goes around and
around until there are thousands of objects on the second free list - it
shows up as a memory leak as it never gets reused or freed.
I am also concerned that there is no mechanism for releasing allocated
memory in the __default_allocator class, but that is a side issue.
The test code is pretty simple (included below), compile with:
g++ -fPIC -c leaklib.cc
g++ -o leaktest leaktest.cc leaklib.o
And running gives this:
Deallocate is: 0x804a420
Deallocate2 is: 0x40031790
And ldd shows that 0x40031790 is in libstdc++
ldd ./leaktest
libstdc++-libc6.0-1.so.2 => /usr/lib/libstdc++-libc6.0-1.so.2 (0x4000f000)
libm.so.6 => /lib/libm.so.6 (0x40053000)
libc.so.6 => /lib/libc.so.6 (0x4006e000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
My platform is Debian 2.1 i386 linux glibc 2.0:
gcc version egcs-2.91.60 Debian 2.1 (egcs-1.1.1 release)
And I have verified the results on Debian 'potato' i386 linux glibc2.1
with,
gcc version egcs-2.91.66 Debian GNU/Linux (egcs-1.1.2 release)
Both of the above work fine if the -fPIC is omitted.
And also Debian 'potato' sparc linux glibc2.1 with,
gcc version egcs-2.91.66 Debian GNU/Linux (egcs-1.1.2 release)
However, the sparc only shows the different symbol addresses, not the
memory leak side-effect, perhaps the free_list symbol is not duplicated...
I was -NOT- able to reproduce this on Debian 'potato' alpha linux glibc2.1
with,
gcc version egcs-2.91.66 Debian GNU/Linux (egcs-1.1.2 release)
Thanks,
Jason
---- leaktest.cc ----
#include <malloc.h>
#include <string>
string get_string ();
void leak ()
{
string tmp = get_string();
}
int main ()
{
cout << "Deallocate is: " <<
(void *)&__default_alloc_template<1, 0>::deallocate << endl;
while (true)
{
leak();
// malloc_stats();
}
return 0;
}
------------
---- leaklib.cc ----
#include <alloc.h>
#include <iostream>
#include <string>
string get_string ()
{
cout << "Deallocate2 is: " <<
(void *)&__default_alloc_template<1, 0>::deallocate << endl;
return "blah";
}