title: How destroy and recreate a lua_State
body: <p>For a school project, I'm using Lua to add some scripting functionality to a game engine.  The engine is quite slow to load, so rather than having to restart it every time I change one of my Lua scripts, I'd like to be able to just reload the scripts.  I'd like to be able to do this in a way which is safe and reliable even if other script authors have polluted the global state in unpredictable ways.</p>
  
  <p>In my mind, the easiest way to go about it is to simply destroy the <code>lua_State</code> in my C++ code, and then create a new one, re-bind my functions and reload all of the necessary scripts.  However, I'm having some trouble getting it work properly.  I have a class which serves as an interface to the Lua virtual machine, which has the following constructor and destructor:</p>
  
  <pre><code>LuaInterface::LuaInterface()
  {
    luaVM = lua_open();
    luaL_openlibs(luaVM);
  
    // Create the ortsgfx table.
    lua_newtable(luaVM);
    lua_setglobal(luaVM, TABLE_NAME);
  }
  
  LuaInterface::~LuaInterface()
  {
    // Close the Lua virtual machine
    lua_close(luaVM);
  }
  </code></pre>
  
  <p>Note that lua_close(luaVM) is called when the object's destructor executes.  I attempt to reset Lua from the game engine with the following code:</p>
  
  <pre><code>lua.~LuaInterface();
  lua = LuaInterface();
  initLua();
  </code></pre>
  
  <p>(Lua is a LuaInterface object, of course.)  This causes a segmentation fault in <code>initLua()</code> when I try to initialize one of my tables.  <strong>However, if I remove the <code>lua_close(luaVM)</code> call in the destructor, then everything works fine.</strong></p>
  
  <p>What am I doing wrong?  Additionally, is there a better way to go about reloading my Lua scripts?</p>