Hi!
I'm working with Visual Studio 2005 and have the following problem in my code:
I have several Structs with member variables. Those Structs are used by several classes as "Parameter classes", so if a member variable is changed, all classes should get the changed variable the next time they call it.
To implement this behaviour I used a Singleton Holder class. Here is the layout of the classes:
In SingletonHolder.h
The Parameter classes are defined in parameter.hCode:template<class T>
class SingletonHolder
{
public:
static T& Instance();
private:
SingletonHolder();
SingletonHolder(const T&);
SingletonHolder& operator=(const T&);
~SingletonHolder();
};
template<class T>
T& SingletonHolder<T>::Instance()
{
static T obj;
return obj;
}
The implementation of Params and OtherParams in parameter.cpp:Code:struct Params
{
private:
double m_val1;
double m_val2;
public:
double getVal1() const { return m_val1; }
double getVal2() const { return m_val2; }
void setVal1(double val) { m_val1 = val; }
void setVal2(double val) { m_val2 = val; }
Params();
};
struct OtherParams
{
private:
double m_otherVal1;
double m_otherVal2;
public:
double getOtherVal1() const { return m_otherVal1; }
double getOtherVal2() const { return m_otherVal2; }
void setOtherVal1(double val) { m_val1 = val; }
void setOtherVal2(double val) { m_val2 = val; }
OtherParams();
};
// only those Types are used by Client classes:
typedef SingletonHolder<Params> SingletonParams;
typedef SingletonHolder<OtherParams> SingletonOtherParams;
Classes can now access those parameters through:Code:Params::Params()
: m_val1(1)
, m_val1(2)
{
// should be called only once (when static object is created)!
cout << "Calling Params()" << endl;
}
OtherParams::OtherParams()
: m_val1(10)
, m_val1(20)
{
// should be called only once (when static object is created)!
cout << "Calling OtherParams()" << endl;
}
Please note, that there should be during program execution only exactly one instance of Params and OtherParams!Code:double val1 = SingletonParams::Instance().getVal1();
double val2 = SingletonOtherParams::Instance().getOtherVal2();
...
My Problem is, that in Debug Mode (in VS2005) everything works as expected but in Release Mode there is one function where suddenly a new Object is created (constructor was called and a second cout notification happens). The code looks like this:
It doesn't matter how the loops look like the thing is the c'tors of Params and OtherParams should never be called twice. As I said problems start in Release Mode and not in Debug mode.Code:SingletonParams::Instance().setVal1(5);
...
cout << SingletonParams::Instance().getVal1(); // OK! is 5
updateSomething(); // Still OK in this function
// first for loop
for (...)
{
cout << SingletonParams::Instance().getVal1(); // Still OK.
}
// some other loop
for (...)
{
cout << SingletonParams::Instance().getVal1(); // Ups, new Object and c'tor is called!
}
Is there something fundamentally wrong with my code? Are the typedefs in the right place? I now that there are other possibilities to implement the wanted behaviour but I'd like to now what my mistake was.
Maybe somebody has an idea?
Thanks!
Marquito