title: Cannot downcast on an object which has been loaded from a .so library
body: <p>I'm currently working on a game engine which implements an architecture based on frameworks.</p>
  
  <p>These frameworks are dynamically loaded into the engine through the using of .so libraries.</p>
  
  <p>To illustrate the frameworks usage, here is a little picture:
  <a href="https://i.stack.imgur.com/lNdgo.png" rel="nofollow noreferrer">Engine/Frameworks interactions</a></p>
  
  <p><em>(please, note that the engine also knows about the FrameworkTpl and SpecificFrameworkTpl types)</em></p>
  
  <p>As we can see, the <em>specificFrameworkImplementation</em> class is compiled as a shared library (.so) and inherit from <em>specificFrameworkTpl</em> which itself inherit from <em>FrameworkTpl</em>.</p>
  
  <p>Once compiled, the library is loaded into the engine at runtime and an instance of the framework is retrieved as a <em>FrameworkTpl</em>.</p>
  
  <p>At this time if I call a <em>FrameworkTpl</em> method, it will works like a charm (which is pretty obvious yes...).</p>
  
  <p>Neverless, if I try to down cast this instance to a <em>specificFrameworkTpl</em> with the help of a <code>dynamic_cast</code>, it will return a <code>nullptr</code>.</p>
  
  <p>Through my research (which have last for about one week while reading mans pages and StackOverflow answers), I saw peoples talking about flags to give to the <code>dlopen</code> function such as <strong>RTLD_NOW</strong> or to remove like <strong>RTLD_LOCAL</strong> in favor of <strong>RTLD_GLOBAL</strong>, but none of them has worked for me, same thing for g++ flags (<code>-rdynamic</code>).</p>
  
  <p><strong>The code:</strong></p>
  
  <p>Here is an example with <code>GuiFrameworkTpl</code> as <code>specificFrameworkTpl</code>
  and <code>FrameworkUI</code> for <code>specificFrameworkImplementation</code>.</p>
  
  <p><em>FrameworkTpl</em></p>
  
  <pre><code>class FrameworkTpl
  {
  protected:
      std::string _name;
  
  public:
      FrameworkTpl(const std::string &amp;name):
          _name(name)
      {
      }
  
      virtual ~FrameworkTpl(void)
      {
      }
  
  public:
      virtual std::string getName(void) const
      {
          return (this-&gt;_name);
      }
  };
  </code></pre>
  
  <p><em>GuiFrameworkTpl</em></p>
  
  <pre><code>class GuiFrameworkTpl : public FrameworkTpl
  {
  public:
      GuiFrameworkTpl(const std::string &amp; name) : 
          FrameworkTpl(name)
      {
      }
      virtual ~GuiFrameworkTpl(void)
      {
      }
  
      virtual void displaySquare(int x, int y, int size) = 0;
  };
  </code></pre>
  
  <p><em>FrameworkUI</em></p>
  
  <pre><code>class FrameworkUI : public GuiFrameworkTpl
  {
  public:
      FrameworkUI();
      ~FrameworkUI();
  
  public:
      void displaySquare(int x, int y, int size);
  };
  
  extern "C"
  {
      FrameworkUI* CObject()
      {
          return (new FrameworkUI);
      }
  
      void DObject(FrameworkUI* obj)
      {
          delete obj;
      }
  }
  </code></pre>
  
  <p>App that uses the library and cast stuff from it:
  <em>(Here, the Xxx from the previous classes is used as "Gui")</em></p>
  
  <pre><code>GuiFrameworkTpl* GUISystem::cast_f(const std::shared_ptr&lt;FrameworkTpl&gt;&amp; src)
  {
      auto ptr = src.get();
      Log::print(ptr);
      if (!ptr) return nullptr;
      Log::inform(ptr-&gt;getName());
      auto dst = dynamic_cast&lt;GuiFrameworkTpl*&gt;(ptr);
      Log::print(dst);
      return dst;
  }
  </code></pre>
  
  <p><strong>What I should have:</strong></p>
  
  <p>When I load my library and call <code>CObject()</code> it should create an instance of <em>FrameworkUI</em> which I get in a <em>FrameworkTpl</em>, then I would just have to dynamic_cast it as a <em>GuiFrameworkTpl</em> in order to access particular method.</p>
  
  <p><strong>What I really have:</strong></p>
  
  <p>When I load my library and call <code>CObject()</code> it creates an instance of <em>FrameworkUI</em> which I store in a <em>FrameworkTpl</em> (btw, I can call the <code>getName()</code> method successfully, so the instance is valid). But when I try to down cast it to <em>GuiFrameworkTpl</em>, it gives me a <code>nullptr</code>.</p>
  
  <p>The last function print this:</p>
  
  <pre><code>0xc0c200
  [INFO]: FrameworkUI
  0
  </code></pre>
  
  <p>If anyone has some clue about how to solve this problem, I would be very grateful,
  Thanks in advance</p>