I am creating a simple plugin structure in which I would like to be able to use dlopen () a shared library (like a plugin), test and use any factory functions and ultimately dlclose (), leaving no traces.
My factory system is trivial, with one exported function that returns a pointer to a common base class. To check if the plugin is loaded correctly, I have a static object whose destructor installs bool from the main program.
Here's the main program:
// dltest.cpp follows. Compile with g++ -std=c++0x dltest.cpp -o dltest -ldl
And the plugin code:
// libempty.cpp follows. Compile with g++ -std=c++0x libempty.cpp -o libempty.so -fPIC -shared
If executed, exit:
false false ~Finilizer()
This shows that the dlclose () call is not working properly, and the library was not unloaded before the program exited.
However, if we move the vector outside of the function, so the last 8 lines are read:
class Foo: public Base { virtual void init() { } }; static const vector<float> ns = { 0.f, 0.75f, 0.67f, 0.87f }; extern "C" __attribute__ ((visibility ("default"))) Base* newBase() { return new Foo; }
Then dlclose () works correctly, and the output is:
false ~Finilizer() true
The same results are generated if the vector remains in the function but the factory is not exported:
class Foo: public Base { virtual void init() { static const vector<float> ns = { 0.f, 0.75f, 0.67f, 0.87f }; } };
Positive results are found if the vector is replaced by an array of C:
class Foo: public Base { virtual void init() { static const float ns[] = { 0.f, 0.75f, 0.67f, 0.87f }; } }; extern "C" __attribute__ ((visibility ("default"))) Base* newBase() { return new Foo; }
Is this a bug in GCC / Linux? Is there any workaround so that complex objects can be statically declared in a factorized class member function?