Index: /trunk/nv/exception.hh
===================================================================
--- /trunk/nv/exception.hh	(revision 255)
+++ /trunk/nv/exception.hh	(revision 256)
@@ -14,7 +14,8 @@
 #define NV_EXCEPTION_HH
 
+#include <nv/common.hh>
+#include <string>
 #include <exception>
 #include <stdexcept>
-#include <string>
 
 namespace nv
@@ -28,5 +29,5 @@
 	{
 	public:
-		explicit logic_error(const std::string&  __arg) : std::logic_error( __arg ) {}
+		explicit logic_error( const std::string& msg ) : std::logic_error( msg ) {}
 	};
 
@@ -39,5 +40,5 @@
 	{
 	public:
-		explicit runtime_error(const std::string&  __arg) : std::runtime_error( __arg ) {}
+		explicit runtime_error( const std::string& msg ) : std::runtime_error( msg ) {}
 	};
 }
Index: /trunk/nv/gfx/texture_font.hh
===================================================================
--- /trunk/nv/gfx/texture_font.hh	(revision 255)
+++ /trunk/nv/gfx/texture_font.hh	(revision 256)
@@ -9,5 +9,5 @@
 
 #include <nv/common.hh>
-#include <string>
+#include <nv/string.hh>
 #include <unordered_map>
 #include <nv/math.hh>
@@ -45,5 +45,5 @@
 			texture_font( texture_atlas* atlas, const char * filename, float size );
 			const texture_glyph* get_glyph( uint16 charcode ) const;
-			bool load_glyphs( const std::string& codes );
+			bool load_glyphs( const string& codes );
 			float get_size() const { return m_size; }
 			~texture_font();
@@ -54,5 +54,5 @@
 
 			texture_atlas* m_atlas; //!< Atlas Image object for this font.
-			std::string m_filename; //!< Name of the file.
+			string m_filename; //!< Name of the file.
 			float m_size;           //!< Font size.
 			float m_height;         //!< Height of the font. (x-height?)
Index: /trunk/nv/interface/vertex_buffer.hh
===================================================================
--- /trunk/nv/interface/vertex_buffer.hh	(revision 255)
+++ /trunk/nv/interface/vertex_buffer.hh	(revision 256)
@@ -18,5 +18,4 @@
 #include <nv/interface/mesh_data.hh>
 #include <unordered_map>
-#include <string>
 
 namespace nv
Index: /trunk/nv/library.hh
===================================================================
--- /trunk/nv/library.hh	(revision 255)
+++ /trunk/nv/library.hh	(revision 256)
@@ -15,5 +15,5 @@
 
 #include <nv/exception.hh>
-#include <string>
+#include <nv/string.hh>
 
 namespace nv
@@ -37,5 +37,5 @@
 		 * Throws library_error on failure
 		 */
-		void open( const std::string& name );
+		void open( const string& name );
 
 		/**
@@ -44,5 +44,5 @@
 		 * returns true if succeeded, false otherwise
 		 */
-		bool try_open( const std::string& name );
+		bool try_open( const string& name );
 
 		/**
@@ -54,5 +54,5 @@
 		 * Returns library name
 		 */
-		const std::string& get_name() const;
+		const string& get_name() const;
 
 		/**
@@ -61,5 +61,5 @@
 		 * Throws on symbol not found
 		 */
-		void* get( const std::string& symbol );
+		void* get( const string& symbol );
 
 		/**
@@ -68,5 +68,5 @@
 		 * Returns null if symbol not found
 		 */
-		void* try_get( const std::string& symbol );
+		void* try_get( const string& symbol );
 
 		/**
@@ -82,5 +82,5 @@
 		 * Exact implementation depends on platform/compiler.
 		 */
-		static std::string get_error();
+		static string get_error();
 
 	protected:
@@ -107,5 +107,5 @@
 
 		/// Library name
-		std::string m_name;
+		string m_name;
 
 	};  // class Library
@@ -114,10 +114,10 @@
 	{
 		/// Library name
-		std::string m_name;
+		string m_name;
 	public:
 		/**
 		 * Constructor
 		 */
-		library_error( const std::string& message, const std::string& name )
+		library_error( const string& message, const string& name )
 			: runtime_error( "Library (" + name + ") : " + message + " [ " + library::get_error() + " ]"), m_name( name )
 		{
@@ -134,5 +134,5 @@
 		 * Returns library name
 		 */
-		const std::string& get_name()
+		const string& get_name()
 		{
 			return m_name;
Index: /trunk/nv/logger.hh
===================================================================
--- /trunk/nv/logger.hh	(revision 255)
+++ /trunk/nv/logger.hh	(revision 256)
@@ -14,8 +14,8 @@
 #define NV_LOGGER_HH
 
-#include <string>
+#include <nv/string.hh>
+#include <nv/logging.hh>
 #include <iosfwd>
 #include <list>
-#include <nv/logging.hh>
 
 namespace nv
Index: /trunk/nv/logging.hh
===================================================================
--- /trunk/nv/logging.hh	(revision 255)
+++ /trunk/nv/logging.hh	(revision 256)
@@ -16,6 +16,6 @@
 #include <nv/common.hh>
 #include <nv/singleton.hh>
+#include <string>
 #include <sstream>
-#include <string>
 
 namespace nv
Index: /trunk/nv/lua/lua_state.hh
===================================================================
--- /trunk/nv/lua/lua_state.hh	(revision 255)
+++ /trunk/nv/lua/lua_state.hh	(revision 256)
@@ -142,4 +142,10 @@
 				register_native_function( detail::function_wrapper< F, f >, name );
 			}
+
+			template < typename C, typename F, F f >
+			struct register_member
+			{
+				register_member( const char* ) { }
+			};
 
 			template < typename C, typename F, F f >
@@ -211,5 +217,6 @@
 
 			void store_metadata( object* o, const std::string& metaname, void* pointer );
-			void unregister_object( object * o );
+			void unregister_object( object * o ); // DELETE ME
+			void unregister_object( int index );
 			void register_native_object_method( const char* lua_name, const char* name, lfunction f );
 			template < typename F, F f >
Index: /trunk/nv/object.hh
===================================================================
--- /trunk/nv/object.hh	(revision 255)
+++ /trunk/nv/object.hh	(revision 256)
@@ -12,4 +12,5 @@
 namespace nv
 {
+
 	/**
 	 * Implements a object tree-like structure.
@@ -18,8 +19,10 @@
 	{
 	public:
+		friend class root;
+
 		/**
 		 * Register the object type in the type database.
 		 */
-		static void register_type( type_database* db );
+		//static void register_type( type_database* db );
 
 		/// List type
Index: /trunk/nv/root.hh
===================================================================
--- /trunk/nv/root.hh	(revision 255)
+++ /trunk/nv/root.hh	(revision 256)
@@ -22,7 +22,14 @@
 		virtual void child_added( object* ) {}
 		virtual void child_removed( object* ) {}
+		virtual void object_created( object* o );
+		virtual void object_destroyed( object* o );
 		lua::state*    get_lua_state()     const { return m_lua_state; }
 		uid_store*     get_uid_store()     const { return m_uid_store; }
-		virtual ~root() { destroy_children(); }
+		virtual ~root() 
+		{ 
+			object_destroyed( this );
+			destroy_children(); 
+			m_root = nullptr; 
+		}
 	protected:
 		lua::state*    m_lua_state;
Index: /trunk/src/gfx/texture_font.cc
===================================================================
--- /trunk/src/gfx/texture_font.cc	(revision 255)
+++ /trunk/src/gfx/texture_font.cc	(revision 256)
@@ -92,5 +92,5 @@
 }
 
-bool texture_font::load_glyphs( const std::string& codes )
+bool texture_font::load_glyphs( const string& codes )
 {
 	FT_Face face     = (FT_Face)(m_rface);
Index: /trunk/src/library.cc
===================================================================
--- /trunk/src/library.cc	(revision 255)
+++ /trunk/src/library.cc	(revision 256)
@@ -4,6 +4,4 @@
 #include "nv/common.hh"
 #include "nv/library.hh"
-
-#include <string.h>
 
 #if NV_PLATFORM == NV_WINDOWS
@@ -41,5 +39,5 @@
 }
 
-void library::open( const std::string& name )
+void library::open( const string& name )
 {
 	m_name = name;
@@ -51,5 +49,5 @@
 }
 
-bool nv::library::try_open( const std::string& name )
+bool nv::library::try_open( const string& name )
 {
 	m_name = name;
@@ -62,5 +60,5 @@
 }
 
-const std::string& library::get_name() const
+const string& library::get_name() const
 {
     return m_name;
@@ -75,6 +73,6 @@
     NV_LOG( LOG_NOTICE, "library : loading '" + m_name + "'..." );
 
-    std::string name = m_name;
-    std::string ext  = NV_LIB_EXT;
+    string name = m_name;
+    string ext  = NV_LIB_EXT;
     size_t ext_len   = ext.length();
 
@@ -95,5 +93,5 @@
 }
 
-void* library::get( const std::string& symbol )
+void* library::get( const string& symbol )
 {
 	void* result = (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.c_str() );
@@ -105,5 +103,5 @@
 }
 
-void* nv::library::try_get( const std::string& symbol )
+void* nv::library::try_get( const string& symbol )
 {
 	return (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.c_str() );
@@ -133,5 +131,5 @@
 }
 
-std::string library::get_error()
+string library::get_error()
 {
 #if NV_PLATFORM == NV_WINDOWS
@@ -140,11 +138,11 @@
     FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
         NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buffer, 0, NULL );
-    std::string msg( (char*)buffer );
+    string msg( (char*)buffer );
     LocalFree( buffer );
     return msg;
 #elif NV_PLATFORM == NV_LINUX || NV_PLATFORM == NV_APPLE
-    return std::string(dlerror());
+    return string(dlerror());
 #else
-    return std::string("");
+    return string("");
 #endif
 }
Index: /trunk/src/lua/lua_state.cc
===================================================================
--- /trunk/src/lua/lua_state.cc	(revision 255)
+++ /trunk/src/lua/lua_state.cc	(revision 256)
@@ -383,12 +383,18 @@
 {
 	if (!o) return;
+	unregister_object( o->get_lua_index() );
+}
+
+void lua::state::unregister_object( int index )
+{
 	stack_guard guard( this );
-	lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
+	lua_rawgeti( m_state, LUA_REGISTRYINDEX, index );
 	lua_pushstring( m_state, "__ptr" );
 	lua_pushboolean( m_state, false );
 	lua_rawset( m_state, -3 );
 	lua_pop( m_state, 1 );
-	luaL_unref( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
-}
+	luaL_unref( m_state, LUA_REGISTRYINDEX, index );
+}
+
 
 void lua::state::deep_pointer_copy( int index, void* obj )
Index: /trunk/src/object.cc
===================================================================
--- /trunk/src/object.cc	(revision 255)
+++ /trunk/src/object.cc	(revision 256)
@@ -41,11 +41,6 @@
 	if ( m_root )
 	{
-		uid_store*  store = get_root()->get_uid_store();
-		if (store)
-		{
-			m_uid = store->insert( this );
-		}
-	}
-
+		m_root->object_created( this );
+	}
 }
 
@@ -104,13 +99,7 @@
 object::~object()
 {
- 	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 );
+	if ( m_root )
+	{
+		m_root->object_destroyed( this );
 	}
 	detach();
@@ -194,16 +183,16 @@
 }
 
-void object::register_type( type_database* db )
-{
-	type_field fields[] = {
-		type_field("id",          &object::m_id),
-		type_field("uid",         &object::m_uid).flag( TF_READONLY ), 
-		type_field("lua_index",   &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
-		type_field("parent",      &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
-		type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
-		type_field("children"   , &object::m_children).flag( TF_READONLY ),
-	};
-	db->create_type<object>("object").fields(fields);
-}
+// void object::register_type( type_database* db )
+// {
+// 	type_field fields[] = {
+// 		type_field("id",          &object::m_id),
+// 		type_field("uid",         &object::m_uid).flag( TF_READONLY ), 
+// 		type_field("lua_index",   &object::m_lua_index).flag( TF_READONLY | TF_NOSERIALIZE ),
+// 		type_field("parent",      &object::m_parent).flag( TF_READONLY | TF_NOSERIALIZE ),
+// 		type_field("child_count", &object::m_child_count).flag( TF_READONLY ),
+// 		type_field("children"   , &object::m_children).flag( TF_READONLY ),
+// 	};
+// 	db->create_type<object>("object").fields(fields);
+// }
 
 void nv::object::register_with_lua( const char* lua_name, const char* storage )
Index: /trunk/src/root.cc
===================================================================
--- /trunk/src/root.cc	(revision 255)
+++ /trunk/src/root.cc	(revision 256)
@@ -0,0 +1,30 @@
+// Copyright (C) 2012-2014 ChaosForge Ltd
+// http://chaosforge.org/
+//
+// This file is part of NV Libraries.
+// For conditions of distribution and use, see copyright notice in nv.hh
+
+#include "nv/root.hh"
+
+#include "nv/uid.hh"
+#include "nv/lua/lua_state.hh"
+
+void nv::root::object_created( object* o )
+{
+	if ( m_uid_store )
+	{
+		o->m_uid = m_uid_store->insert( o );
+	}
+}
+
+void nv::root::object_destroyed( object* o )
+{
+	if ( m_lua_state && o->m_lua_index != lua::ref_none )
+	{
+		m_lua_state->unregister_object( o );
+	}
+	if ( m_uid_store && o->m_uid != 0 )
+	{
+		m_uid_store->remove( o->m_uid );
+	}
+}
