Thanks in advance. I'm now developing a plugin for a big system in C++. In my plugin I have some static variable. I find that when it is compiled in debug mode on Linux, it works fine without any problem. When it is compiled in RELEASE mode, namely some optimization is done by the compiler, then when I unload the plugin, the static variable is not deleted ( the destructor of the static variable class is never called.) so the memory is never released and next time when I reload the plugin, it causes the main program crash!
Can anybody explain me why the static variable is not destroyed when the plugin is unloaded? NOTE: the static variable is a static instance, not a pointer!
class MySettings
{
public:
static MySettings& Instance() {
static MySettings theSingleton;
return theSingleton;
}
virtual ~MySettings();
}
in the plugin somewhere, it is called like this
....
MySettings &s = MySettings::Instance();
s.xxx();
....
When I compiled and run in debug mode, I printed some information from the destructor, it looks like the instance is destructred properly, when the plugin is unpluged. But when I compile and run in release mode, the destructor is never called when the plugin is unpluged. I'm not the plugin manager developer, cannot tell too much about it. Thanks a lot for your help!
Here is the piece of code which loaded the plugin libs.
newLib._libHandle = ::dlopen(path_to_the_plugin_lib, RTLD_LAZY | RTLD_GLOBAL);
if(! newLib._libHandle) {
cerr << "dlopen failed for: " << path << " - "
<< ::dlerror();
return "";
I finally get it work. But still don't understand why. Here is what I did:
class MySettings
{
public:
static MySettings& Instance() {
return theSingleton;
}
private:
static MySettings theSingleton;
virtual ~MySettings();
}
MySettings MySettins:theSingleton;
Sinece the application is very big with millions of lines of code. My doubt is that when gcc compiles in RELEASE mode, something goes wrong with the optimization.