考虑使用g ++构建一个动态加载的库下面的代码之前调用 - 在Linux上,-fPIC
4.7与-rdynamic
选项链接:静态变量的析构函数库的析构函数
struct Wrapper
{
libraryUnregisterCbMap_t instance;
Wrapper() : instance() { HDebugLog("Wrapper CTOR!");}
~Wrapper() { HDebugLog("Wrapper DESTRUCTOR!"); }
};
inline libraryUnregisterCbMap_t& getLibraryUnregisterMap()
{
static Wrapper unregisterLibraryMap;
HDebugLog("getLibraryUnregisterMap: we have " <<unregisterLibraryMap.instance.size() << " elements. the address of the map is " << &unregisterLibraryMap.instance);
return unregisterLibraryMap.instance;
}
void registerLibrary(callbackContainer_t* p)
{
auto& map = getLibraryUnregisterMap();
}
void unregisterLibrary()
{
auto& map = getLibraryUnregisterMap();
}
void __attribute__ ((constructor)) library_init()
{
static callbackContainer_t cbContainer;
HDebugLog("Library constructor: address of static cbContainer is: " << &cbContainer);
registerLibrary(&cbContainer);
}
void __attribute__ ((destructor)) library_fini()
{ unregisterLibrary(); }
对我来说是有趣/恼人的是,我调用lt_dlclose
后没有调用library_fini(),所以对于最终确定来说似乎没用,因为当我在运行期间加载此模块时,Wrapper
实例的析构函数发生之前调用library_fini
。不用说,这种默认行为没有任何意义。
我该如何改变这种毫无意义的行为?我需要在我的库定稿例程中完成我的静态数据。为什么lt_dlclose
未调用library_fini()
?