dlclose() doesn't unload any .so that uses Boost

J j@jabster.pl
Mon May 30 23:07:00 GMT 2011


Hi.

I've been told on the GCC mailing list to ask here. Please consider
the following code:

// host.cpp
// compile with: g++ -ldl host.cpp -o host
#include <stdio.h>
#include <dlfcn.h>

int main( int argc, char ** argv )
{
   printf( "host: Loading libchild.so...\n" );
   void * so = dlopen( "./libchild.so", RTLD_LOCAL | RTLD_NOW );
   printf( "host: so = %p\n", so );

   if( so == 0 ) return 1;

   printf( "host: Unloading libchild.so...\n" );
   if( dlclose( so ) != 0 )
   {
       printf( "host: Error: %s\n", dlerror() );
       return 1;
   }
   printf( "host: Unloaded.\n" );
   printf( "host: %p\n", dlopen( "./libchild.so", RTLD_NOLOAD ) );

   return 0;
}

// child.cpp
// compile with: g++ -lboost_signals child.cpp -shared -fPIC -o libchild.so
#include <stdio.h>
#include <boost/signals.hpp>

struct foobar_t
{
   boost :: signal< void () > signal;
};

void  __attribute__ ( ( constructor ) ) ctor()
{
   foobar_t foobar;

   printf( "child: Constructor\n" );
}

void  __attribute__ ( ( destructor ) ) dtor()
{
   printf( "child: Destructor\n" );
}


This is the output I get:

host: Loading libchild.so...
child: Constructor
host: so = 0x94b6020
host: Unloading libchild.so...
host: Unloaded.
host: (nil)
child: Destructor

As you can see, dlclose() doesn't properly unload the .so, even though
it claims that it has done so. If you'll remove this line:

boost :: signal< void () > signal;

it works properly:

host: Loading libchild.so...
child: Constructor
host: so = 0x8f6f020
host: Unloading libchild.so...
child: Destructor
host: Unloaded.
host: (nil)

It took me a while to track this down. Now, my question is - is this a
bug? If so, in what? In GCC? (I tried this with both 4.5 and 4.6) In
the dynamic linker? In Boost? If not - why? And how would I fix this?
Any insight in greatly appreciated.

Thanks.



More information about the Binutils mailing list