title: dlclose crashes when copying dynamic libraries
body: <p>I have an interesting problem that seems to be unresolved by my research on the internet.
  I'm trying to load libraries dynamically in my c++ project with the functions from dlfcn.h. The problem is that when I try to reload the plugins at running time (because I made a change on any of them), the main program crashes (Segmentation fault (core dumped)) when dlclose() is called.
  Here is my example that reproduces the error:</p>
  
  <p>main.cpp:</p>
  
  <pre><code>#include    &lt;iostream&gt;
  #include    &lt;dlfcn.h&gt;
  #include    &lt;time.h&gt;
  #include    "IPlugin.h"
  
  int main( )
  {
      void * lib_handle;
      char * error;
  
      while( true )
      {
          std::cout &lt;&lt; "Updating the .so" &lt;&lt; std::endl;
  
          lib_handle = dlopen( "./test1.so", RTLD_LAZY );
          if ( ! lib_handle ) 
          {
              std::cerr &lt;&lt; dlerror( ) &lt;&lt; std::endl;
              return 1;
          }
  
          create_t fn_create = ( create_t ) dlsym( lib_handle, "create" );
  
          if ( ( error = dlerror( ) ) != NULL )  
          {
              std::cerr &lt;&lt; error &lt;&lt; std::endl;
              return 1;
          }
  
          IPlugin * ik = fn_create( );
          ik-&gt;exec( );
  
          destroy_t fn_destroy = ( destroy_t ) dlsym( lib_handle, "destroy" );
  
          fn_destroy( ik );
  
          std::cout &lt;&lt; "Waiting 5 seconds before unloading..." &lt;&lt; std::endl;
  
          sleep( 5 );
          dlclose( lib_handle );
      }
  
      return 0;
  }
  </code></pre>
  
  <p>IPlugin.h:</p>
  
  <pre><code>class IPlugin
  {
  public:
      IPlugin( ) { } 
      virtual ~IPlugin( ) { }
      virtual void exec( ) = 0;
  };
  
  typedef IPlugin * ( * create_t  )( );
  typedef void ( * destroy_t  )( IPlugin * );
  </code></pre>
  
  <p>Test1.h:</p>
  
  <pre><code>#include    &lt;iostream&gt;
  #include    "IPlugin.h"
  
  class Test1 : public IPlugin
  {
  public:
      Test1( );
      virtual ~Test1( );
  
      void exec( );
  };
  </code></pre>
  
  <p>Test1.cpp:</p>
  
  <pre><code>#include "Test1.h"
  
  Test1::Test1( ) { }
  
  Test1::~Test1( ) { }
  
  void Test1::exec( )
  {
      std::cout &lt;&lt; "void Test1::exec( )" &lt;&lt; std::endl;
  }
  
  extern "C"
  IPlugin * create( )
  {
      return new Test1( );
  }
  
  extern "C"
  void destroy( IPlugin * plugin )
  {
      if( plugin != NULL )
      {
          delete plugin;
      }
  }
  </code></pre>
  
  <p>To compile:</p>
  
  <pre><code>g++ main.cpp -o main -ldl
  g++ -shared -fPIC Test1.cpp -o plugin/test1.so
  </code></pre>
  
  <p>The problem occurs when for example I change something on the Test1::exec method (changing the string to be printed or commenting the line) and while the main program sleeps I copy the new test1.so to main running directory (cp). If I use the move command (mv), no error occurs. What makes the difference between using cp or mv? Is there any way to solve this problem or to do that using cp?</p>
  
  <p>I'm using Fedora 14 with g++ (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4).</p>
  
  <p>Thanks in advance.</p>