windows .dll vs. linux .so - global static objects
Hi,
a singleton (static, global) object is used from the main app as well as from an dynamical loaded .so/.dll
I observe the following differences in behavior under linux and windows:
under linux the object is created once, at the first call to the singletons instance function (i checked with debug output in the ctor of the class)
but under windows the object is created twice: one time when the main app calls instance() the first time and a second time when instance() is called from the dll for the first time. As you can imagine that behavior really sucks if you want to access consistent data stored in the singleton object from the main app as well as from the dll.
Is there any way to get the behavior under windows/dll exact like under linux/so?
Thank you.
windows .dll vs. linux .so - global static objects
Hi,
Even i am facing the same problem with static objects which is created twice in windows..
I tried all solutions which you have suggested but din't work out..
It doesn't work even when i use without DLL's..
Can anyone please help me out on this??
Expecting for an early reply from you..
Thanks in advance!!
windows .dll vs. linux .so - global static objects
Code:
Hi,
Thanks for the reply!
I have followed the approach as per your suggestion and have come up with few doubts. Please correct me if i am wrong in any step.
Find my sample code below:
sample.hpp:
class sample;
//extern sample* singleton_object;
sample* instance();
class sample{
public:
int i;
private:
//sample();
};
sample.cpp:
#include "sample.hpp"
sample* singleton_object = new sample;
sample* instance(){return singleton_object;}
Here are the doubts:
i> Can i pl know what is the intended use of extern in your suggestion?
Because even without using extern my code works fine.
ii> Also the constructor is not private in this approach. so, how the class can be made singleton?
Please help me out!
Awaiting for your reply..