|
From: stephan b. <sg...@us...> - 2004-12-24 23:03:49
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7574/src/System Modified Files: SharedLib.dl.cpp SharedLib.ltdl.cpp Log Message: Removed the automatic ".so" addition on filenames. Added dl init code, as per the dl/ltdl docs. DL: removed the dlclose() from dtor, for reasons just shown on the mailing list. Index: SharedLib.ltdl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.ltdl.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- SharedLib.ltdl.cpp 23 Dec 2004 21:50:44 -0000 1.2 +++ SharedLib.ltdl.cpp 24 Dec 2004 23:03:40 -0000 1.3 @@ -24,9 +24,6 @@ #include <ltdl.h> #include <errno.h> -#ifndef PCLASSES_WITH_STL -# error "This header requires PCLASSES_WITH_STL to be defined." -#endif #include <string> #include <sstream> #include <map> @@ -36,6 +33,13 @@ namespace System { + void ltdl_init() + { + lt_dlinit(); + lt_dlopen( 0 ); + } + int ltdl_init_placeholder = (ltdl_init(),0); + typedef unsigned long handle_type; typedef std::map<handle_type,lt_dlhandle> lt_handle_map_t; @@ -47,48 +51,54 @@ return PHX::instance(); } -int BindMode2Flags(SharedLib::BindMode mode) -{ // ltdl doesn't use dlopen() flags - return 0; -} + int BindMode2Flags(SharedLib::BindMode mode) + { // ltdl doesn't use dlopen() flags + return 0; + } + + SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) : _handle(0) { - Unicode::String realName = name; - realName.append(".so"); +// Please don't do this: +// Unicode::String realName = name; +// realName.append(".so"); //@fixme _handle = (unsigned long)dlopen(realName.utf8(), BindMode2Flags(mode)); if(!_handle) throw SystemError(errno, lt_dlerror(), P_SOURCEINFO); } -#ifdef PCLASSES_WITH_STL + +#ifndef CERR +#define CERR std::cerr << __FILE__ << ":" << std::dec << __LINE__ << " : " +#endif + + SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) : _handle(0) { - std::ostringstream realName; - realName << name; - realName << ".so"; - lt_dlhandle h = lt_dlopen(realName.str().c_str() /** BindMode2Flags(mode) */ ); + lt_dlhandle h = lt_dlopen(name.c_str() /** BindMode2Flags(mode) */ ); if( h ) { _handle = reinterpret_cast<handle_type>( static_cast<void *>( h ) ); lt_handle_map().insert( std::make_pair( _handle, h ) ); - } - if(!_handle) + } else + { + // CERR << "SharedLib("<<name<<") failed.\n"; throw SystemError(errno, lt_dlerror(), P_SOURCEINFO); + } } -#endif SharedLib::~SharedLib() throw() { lt_handle_map_t::iterator it = lt_handle_map().find( _handle ); if( lt_handle_map().end() != it ) { -// nononono! -// lt_dlclose((*it).second); + CERR << "~SharedLib() erasing a handle: " << _handle << "\n"; +// lt_dlclose((*it).second); lt_handle_map().erase( it ); } } Index: SharedLib.dl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.dl.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- SharedLib.dl.cpp 23 Dec 2004 21:50:44 -0000 1.5 +++ SharedLib.dl.cpp 24 Dec 2004 23:03:40 -0000 1.6 @@ -23,15 +23,19 @@ #include <dlfcn.h> #include <errno.h> -#ifdef PCLASSES_WITH_STL -# include <string> -# include <sstream> -#endif +#include <string> +#include <sstream> namespace P { namespace System { +void libdl_init() +{ + dlopen( 0, RTLD_NOW | RTLD_GLOBAL ); +} +int libdl_init_placeholder = (libdl_init(),0); + int BindMode2Flags(SharedLib::BindMode mode) { int flags = 0; @@ -51,30 +55,50 @@ SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) { - Unicode::String realName = name; - realName.append(".so"); +// Please don't do this: +// Unicode::String realName = name; +// realName.append(".so"); //@fixme _handle = (unsigned long)dlopen(realName.utf8(), BindMode2Flags(mode)); if(!_handle) throw SystemError(errno, dlerror(), P_SOURCEINFO); } -#ifdef PCLASSES_WITH_STL SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) { - std::ostringstream realName; - realName << name; - realName << ".so"; - - _handle = (unsigned long)dlopen(realName.str().c_str(), BindMode2Flags(mode)); + _handle = (unsigned long)dlopen(name.c_str(), BindMode2Flags(mode)); if(!_handle) throw SystemError(errno, dlerror(), P_SOURCEINFO); } -#endif SharedLib::~SharedLib() throw() { - dlclose((void*)_handle); +// dlclose((void*)_handle); +/***** +dlclose() is a BAD IDEA: + +Post-main(): + +#0 0x4001272b in strlen () from /lib/ld-linux.so.2 +#1 0x4000ce20 in _dl_signal_error () from /lib/ld-linux.so.2 +#2 0x40331947 in _dl_close () from /lib/tls/libc.so.6 +#3 0x40370d1a in dlclose_doit () from /lib/libdl.so.2 +#4 0x4000cd56 in _dl_catch_error () from /lib/ld-linux.so.2 +#5 0x40371085 in _dlerror_run () from /lib/libdl.so.2 +#6 0x40370cf0 in dlclose () from /lib/libdl.so.2 +#7 0x400398eb in ~SharedLib (this=0x8053c60) at SharedLib.dl.cpp:77 +#8 0x0804c9d8 in ~PluginCleaner (this=0x8051ec4) at Plugin.h:43 +#9 0x0804ea7c in ~PluginManager (this=0x8051ec0) at Plugin.h:106 +#10 0x0804d8cf in ~Phoenix (this=0x8051ec0) at Phoenix.h:196 +#11 0x0804bce5 in __tcf_1 () at Phoenix.h:161 +#12 0x40271820 in exit () from /lib/tls/libc.so.6 +#13 0x4025cb1a in __libc_start_main () from /lib/tls/libc.so.6 +#14 0x0804a1f1 in _start () at start.S:119 + +HOWEVER, not doing a dlclose() causes: +*** glibc detected *** double free or corruption: 0x08053c60 *** + +*****/ } void* SharedLib::operator[](const char* symbol) throw(RuntimeError) |