Index: trunk/src/gui/gui_element.cc
===================================================================
--- trunk/src/gui/gui_element.cc	(revision 75)
+++ trunk/src/gui/gui_element.cc	(revision 77)
@@ -13,5 +13,5 @@
 
 element::element( root* aroot, const rectangle r ) 
-	: object( aroot, "", 0 ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true )
+	: object( aroot, "" ), m_class(""), m_relative( r ), m_absolute( r ), m_enabled( true ), m_visible( true ), m_dirty( true )
 {
 
Index: trunk/src/lua/lua_state.cc
===================================================================
--- trunk/src/lua/lua_state.cc	(revision 75)
+++ trunk/src/lua/lua_state.cc	(revision 77)
@@ -10,4 +10,6 @@
 #include "nv/logging.hh"
 #include "nv/string.hh"
+#include "nv/root.hh"
+#include "nv/types.hh"
 
 using namespace nv;
@@ -286,3 +288,67 @@
 }
 
-
+lua::reference lua::state::register_object( object * o )
+{
+	if (!o) return ref_none;
+	type_database *db = o->get_root()->get_type_database();
+	if (!db) return ref_none;
+	type_entry* t = db->get_type(typeid(o));
+	if (!t) return ref_none;
+	stack_guard guard( this );
+	lua_getglobal( L, t->name.text );
+	if ( lua_isnil( L, -1 ) )
+	{
+		NV_THROW( runtime_error, std::string( t->name.text ) + " type not registered!" );
+	}
+	deep_pointer_copy( -1, o );
+    return luaL_ref( L, LUA_REGISTRYINDEX );
+}
+
+void lua::state::unregister_object( object * o )
+{
+	if (!o) return;
+	stack_guard guard( this );
+	lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
+	lua_pushstring( L, "__ptr" );
+	lua_pushboolean( L, false );
+	lua_rawset( L, -3 );
+	lua_pop( L, 1 );
+	luaL_unref( L, LUA_REGISTRYINDEX, o->get_lua_index() );
+}
+
+void lua::state::deep_pointer_copy( int index, void* obj )
+{
+	index = lua_absindex( L, index );
+	lua_newtable( L );
+	lua_pushnil( L );
+	bool has_functions = false;
+	bool has_metatable = false;
+
+	while ( lua_next( L, index ) != 0 )
+	{
+		if ( lua_isfunction( L, -1 ) ) 
+			has_functions = true;
+		else if ( lua_istable( L, -1 ) )
+		{
+			deep_pointer_copy( -1, obj );
+			lua_insert( L, -2 );
+			lua_pop( L, 1 );
+		}
+		lua_pushvalue( L, -2 );
+		lua_insert( L, -2 );
+		lua_settable( L, -4 );
+	}
+
+	if ( lua_getmetatable( L, -2 ) )
+	{
+		lua_setmetatable( L, -2 );
+		has_metatable = true;
+	}
+
+	if ( has_functions || has_metatable )
+	{
+		lua_pushstring( L, "__ptr" );
+		lua_pushlightuserdata( L, obj );
+		lua_rawset( L, -3 );
+	}
+}
Index: trunk/src/object.cc
===================================================================
--- trunk/src/object.cc	(revision 75)
+++ trunk/src/object.cc	(revision 77)
@@ -9,4 +9,6 @@
 #include "nv/root.hh"
 #include "nv/types.hh"
+#include "nv/lua/lua_state.hh"
+#include "nv/uid.hh"
 
 using namespace nv;
@@ -19,8 +21,21 @@
 }
 
-object::object( root* aroot, const string& aid, uid auid )
-	: m_root( aroot ), m_id(aid), m_name(), m_uid( auid ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
+object::object( root* aroot, const string& aid )
+	: m_root( aroot ), m_id(aid), m_name(), m_uid( 0 ), m_lua_index(-2), m_parent( nullptr ), m_children(), m_child_count(0)
 {
-	//	uid_store::register_object( this, auid );
+	if ( m_root )
+	{
+		uid_store*  store = get_root()->get_uid_store();
+		lua::state* state = get_root()->get_lua_state();
+		if (store)
+		{
+			m_uid = store->insert( this );
+		}
+		if (state)
+		{
+			m_lua_index = state->register_object( this );
+		}
+	}
+
 }
 
@@ -77,9 +92,14 @@
 object::~object()
 {
-// 	if ( m_lua_index != lua::ref_none )
-// 	{
-// 		lua::state::get()->unregister_object( this );
-// 	}
-// 	uid_store::unregister_object( m_uid );
+ 	if ( m_lua_index != lua::ref_none )
+ 	{
+		lua::state* state = get_root()->get_lua_state();
+ 		state->unregister_object( this );
+ 	}
+	if ( m_uid != 0 && m_root )
+	{
+		uid_store* store = get_root()->get_uid_store();
+		if (store) store->remove( m_uid );
+	}
 	detach();
 	destroy_children();
