title: dlsym causing segmentation fault
body: <p>So I've been trying to get symbols from a local dylib but whenever I try to do anything I just get a seg fault and I'm unsure why this occurs. I've seen this done in other programs and they work fine, so I know it works but I just can't seem to do it :(. Any help is appreciated.
  Cheers</p>
  
  <pre class="lang-c++ prettyprint-override"><code>typedef void (*func_t)();
  func_t testFunction;
  
  
  template&lt;class type&gt;
  type findSymbol(string dylib, string symbol)
  {
      void* handle = dlopen(dylib.c_str(), RTLD_NOW);
  
      if(!handle)
      {
          debug(dlerror());
          return nullptr;
      }
  
      // Reset errors
      dlerror();
  
      type sym = (type)dlsym(handle, symbol.c_str());
  
      const char* dlsym_error = dlerror();
      if(dlsym_error)
      {
          debug(dlsym_error);
          dlclose(handle);
          return nullptr;
      }
  
      sym(); &lt;--- Executes fine
  
      dlclose(handle);
  
      return sym;
  }
  
  int main()
  {        
      // Example 1
      testFunction = findSymbol&lt;func_t&gt;("./test.dylib", "hello"); &lt;--- Seg fault
  
      // Example 2
      func_t f = findSymbol&lt;func_t&gt;("./test.dylib", "hello"); &lt;--- Fine
      f(); &lt;--- Seg fault
  
      return EXIT_SUCCESS;
  }
  </code></pre>