title: Lua function crashes when comparing stored C++ object pointers using Luabind
body: <p>I'm just getting started with Luabind and C++.  My goal is pretty simple:</p>
  
  <p>I want to create a Lua function that takes a C++ object pointer as an argument and stores the object in a Lua variable.  Each time the Lua function is called, it should first check if the object pointer passed to it is the same object pointer as the one stored during the previous call.</p>
  
  <p>Here is the full code:</p>
  
  <pre><code>extern "C" {
    #include "lua.h"
    #include "lualib.h"
  }
  #include &lt;luabind/luabind.hpp&gt;
  
  class Obj {
  };
  
  int main(int argc, char **argv) {
    lua_State* cLuaState = luaL_newstate();
    luabind::open(cLuaState);
    luaL_openlibs(cLuaState);
  
    luabind::module(cLuaState) [
      luabind::class_&lt;Obj&gt;("Obj")
    ];
  
    luaL_dostring(cLuaState, "\
      function func(v)\n\
        print (\"Before: x is same as v?\")\n\
        print (x == v)\n\
        x = v\n\
        print (\"After: x is same as v?\")\n\
        print (x == v)\n\
      end");
  
    Obj* o = new Obj();
    try {
      luabind::call_function&lt;void&gt;(cLuaState, "func", o);
      luabind::call_function&lt;void&gt;(cLuaState, "func", o);
    } catch (std::exception &amp;e) {
      std::cout &lt;&lt; "Exception thrown: " &lt;&lt; e.what() &lt;&lt; std::endl;
      return 1;
    }
    return 0;
  }
  </code></pre>
  
  <p>When I run this program, I would expect to see the following output:</p>
  
  <pre><code>Before: x is same as v?
  false
  Setting v
  After: x is same as v?
  true
  Before: x is same as v?
  true 
  Setting v
  After: x is same as v?
  true
  </code></pre>
  
  <p>However, when I run the program, it crashes during the second call to the Lua function during the comparison between 'x' and 'v'.  This is the actual resulting output from the program:</p>
  
  <pre><code>Before: x is same as v?
  false
  Setting v
  After: x is same as v?
  true
  Before: x is same as v?
  Exception thrown: lua runtime error
  *** Exited with return code: 1 ***
  </code></pre>
  
  <p>As can be seen, the comparison works during the first function call both before and after 'v' is set.  However, the first comparison fails during the second function called.</p>
  
  <p>I must be missing something incredibly obvious, but I really can't figure out what it is.  Can anyone see what I'm doing wrong?  Any advice is much appreciated!</p>
  
  <p>Thank you very much,
  Martin</p>