From cc6e599a52b3a062e62eb1a225c603f105e95f04 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 5 Oct 2014 21:59:17 +0200 Subject: added userdata to destroy function, remembering userdata on module initialization and passing it to destroy function in principle the system works, a dlopen of the lua module removes necessary functions needed during lua VM shutdown, this is due to stack unwinding and the order objects are destroyed (should be fixable) --- include/luaglue/LuaVM.hpp | 1 + include/module/ModuleLoader.hpp | 7 +++++-- include/module/ModuleRegistry.hpp | 20 ++++++++++---------- src/libluaglue/LuaVM.cpp | 5 +++++ tests/modules/test4.MUST | 2 +- tests/modules/testmod4/TestMod4.cpp | 10 ++++++++-- tests/tolua/libtest1/TestMod.cpp | 5 ++++- tests/tolua/test1.cpp | 7 +------ 8 files changed, 35 insertions(+), 22 deletions(-) diff --git a/include/luaglue/LuaVM.hpp b/include/luaglue/LuaVM.hpp index f25cc3e..baa5bca 100755 --- a/include/luaglue/LuaVM.hpp +++ b/include/luaglue/LuaVM.hpp @@ -14,6 +14,7 @@ class LuaVM void loadSource( const char *sourceFilename ); void executeMain( ); void dumpState( ); + void fullGarbageCollect( ); lua_State *handle( ); diff --git a/include/module/ModuleLoader.hpp b/include/module/ModuleLoader.hpp index 21905ec..045383f 100755 --- a/include/module/ModuleLoader.hpp +++ b/include/module/ModuleLoader.hpp @@ -28,6 +28,7 @@ struct Module { #else HMODULE handle; #endif + void *user_data; ModuleRegistry< Interface, CtorParams > *registry; }; @@ -81,9 +82,11 @@ class BaseModuleLoader { } if( m.registry->initModule != 0 ) { - m.registry->initModule( user_data ); + m.registry->initModule( user_data ); } + m.user_data = user_data; + m_modules.insert( std::make_pair( m.registry->name, m ) ); } } @@ -96,7 +99,7 @@ class BaseModuleLoader { typename mapType::iterator it = m_modules.begin( ); if( (*it).second.registry ) { if( (*it).second.registry->destroyModule != 0 ) { - (*it).second.registry->destroyModule( ); + (*it).second.registry->destroyModule( (*it).second.user_data ); } } if( (*it).second.handle ) { diff --git a/include/module/ModuleRegistry.hpp b/include/module/ModuleRegistry.hpp index d98c9ba..058aa24 100755 --- a/include/module/ModuleRegistry.hpp +++ b/include/module/ModuleRegistry.hpp @@ -12,13 +12,13 @@ template< typename Interface> struct ModuleRegistry< Interface > { std::string name; void (*initModule)( void *user_data ); - void (*destroyModule)( ); + void (*destroyModule)( void *user_data ); Interface *(*create)( ); void (*destroy)( Interface *obj ); ModuleRegistry( std::string _name, void (*_initModule)( void *user_data ), - void (*_destroyModule)( ), + void (*_destroyModule)( void *user_data ), Interface *(*_create)( ), void (*_destroy)( Interface *obj ) ) : name( _name ), @@ -32,13 +32,13 @@ template< typename Interface, typename P1 > struct ModuleRegistry< Interface, TYPELIST_1( P1 ) > { std::string name; void (*initModule)( void *user_data ); - void (*destroyModule)( ); + void (*destroyModule)( void *user_data ); Interface *(*create)( P1 ); void (*destroy)( Interface *obj ); ModuleRegistry( std::string _name, void (*_initModule)( void *user_data ), - void (*_destroyModule)( ), + void (*_destroyModule)( void *user_data ), Interface *(*_create)( P1 ), void (*_destroy)( Interface *obj ) ) : name( _name ), @@ -52,13 +52,13 @@ template< typename Interface, typename P1, typename P2 > struct ModuleRegistry< Interface, TYPELIST_2( P1, P2 ) > { std::string name; void (*initModule)( void *user_data ); - void (*destroyModule)( ); + void (*destroyModule)( void *user_data ); Interface *(*create)( P1, P2 ); void (*destroy)( Interface *obj ); ModuleRegistry( std::string _name, void (*_initModule)( void *user_data ), - void (*_destroyModule)( ), + void (*_destroyModule)( void *user_data ), Interface *(*_create)( P1, P2 ), void (*_destroy)( Interface *obj ) ) : name( _name ), @@ -72,13 +72,13 @@ template< typename Interface, typename P1, typename P2, typename P3 > struct ModuleRegistry< Interface, TYPELIST_3( P1, P2, P3 ) > { std::string name; void (*initModule)( void *user_data ); - void (*destroyModule)( ); + void (*destroyModule)( void *user_data ); Interface *(*create)( P1, P2, P3 ); void (*destroy)( Interface *obj ); ModuleRegistry( std::string _name, void (*_initModule)( void *user_data ), - void (*_destroyModule)( ), + void (*_destroyModule)( void *user_data ), Interface *(*_create)( P1, P2, P3 ), void (*_destroy)( Interface *obj ) ) : name( _name ), @@ -92,13 +92,13 @@ template< typename Interface, typename P1, typename P2, typename P3, typename P4 struct ModuleRegistry< Interface, TYPELIST_4( P1, P2, P3, P4 ) > { std::string name; void (*initModule)( void *user_data ); - void (*destroyModule)( ); + void (*destroyModule)( void *user_data ); Interface *(*create)( P1, P2, P3, P4 ); void (*destroy)( Interface *obj ); ModuleRegistry( std::string _name, void (*_initModule)( void *user_data ), - void (*_destroyModule)( ), + void (*_destroyModule)( void *user_data ), Interface *(*_create)( P1, P2, P3, P4 ), void (*_destroy)( Interface *obj ) ) : name( _name ), diff --git a/src/libluaglue/LuaVM.cpp b/src/libluaglue/LuaVM.cpp index cf8935f..d587897 100644 --- a/src/libluaglue/LuaVM.cpp +++ b/src/libluaglue/LuaVM.cpp @@ -28,6 +28,11 @@ void LuaVM::initialize( ) luaL_openlibs( m_lua ); } +void LuaVM::fullGarbageCollect( ) +{ + lua_gc( m_lua, LUA_GCCOLLECT, 0 ); +} + void LuaVM::loadSource( const char *sourceFilename ) { int res; diff --git a/tests/modules/test4.MUST b/tests/modules/test4.MUST index 1d14a0c..9ef1168 100644 --- a/tests/modules/test4.MUST +++ b/tests/modules/test4.MUST @@ -2,5 +2,5 @@ Created common object test4: Module 4 initModule called with user data: 47 hello there test4: hello from main test4: hello world from module -test4: Module 4 destroyModule called +test4: Module 4 destroyModule called with user data: 47 hello there Destroyed common object diff --git a/tests/modules/testmod4/TestMod4.cpp b/tests/modules/testmod4/TestMod4.cpp index de8b1b6..f2d0ecd 100755 --- a/tests/modules/testmod4/TestMod4.cpp +++ b/tests/modules/testmod4/TestMod4.cpp @@ -34,9 +34,15 @@ static void initModule( void *user_data ) Common::instance( ).print( ss.str( ) ); } -static void destroyModule( ) +static void destroyModule( void *user_data ) { - Common::instance( ).print( "Module 4 destroyModule called" ); + UserData *data = (UserData *)user_data; + ostringstream ss; + + ss << "Module 4 destroyModule called with user data: " + << data->version << " " << data->text; + + Common::instance( ).print( ss.str( ) ); } REGISTER_MODULE( "testmod4", &initModule, &destroyModule, Base, Derived ) diff --git a/tests/tolua/libtest1/TestMod.cpp b/tests/tolua/libtest1/TestMod.cpp index 912d652..b06644e 100755 --- a/tests/tolua/libtest1/TestMod.cpp +++ b/tests/tolua/libtest1/TestMod.cpp @@ -19,8 +19,11 @@ static void initModule( void *user_data ) tolua_TestMod_open( luaVm->handle( ) ); } -static void destroyModule( ) +static void destroyModule( void *user_data ) { + LuaVM *luaVm = (LuaVM *)user_data; + + luaVm->fullGarbageCollect( ); } REGISTER_MODULE( "testmod", &initModule, &destroyModule, Base, Derived ) diff --git a/tests/tolua/test1.cpp b/tests/tolua/test1.cpp index 902b278..271544e 100755 --- a/tests/tolua/test1.cpp +++ b/tests/tolua/test1.cpp @@ -23,13 +23,8 @@ int main( int /* argc */, char *argv[] ) luaVm.loadSource( argv[1] ); luaVm.executeMain( ); - //luaVm.dumpState( ); - - //Base *obj = loader.create( "testmod" ); - //obj->hello( ); - //loader.destroy( obj ); - return 0; + } catch( exception &e ) { LOG( logFATAL ) << "ERROR: " << e.what( ); return 1; -- cgit v1.2.3-54-g00ecf