Initialisation and cleanup

A

Anonymous

I have a class (classA) that requires initialisation ONCE. It also
provides static methods that depend on various variables etc to have
been correctly initialised.

Some sample code to explain the scenario:

class ClassA
{
public:
~ClassA();
static ClassB * fetchB();
static ClassC * fetchC();

private:
ClassA();
static int dummy ;
};


//Impl
namespace
{
int MyInitStuf(){ return 42 ;} //assume this allocs memory
void Cleanup(); //used to clean up previously allocd mem
};

static int ClassA::dummy = MyInitStuf();


I have two questions re the code above:

1). Is the dummy variable GUARANTEED to have been initialised already if
I call one of the static methods (or are the satic variables first
initialised when an INSTANCE is created ?

2). Supposing that the MyInitStuf() function allocates memory etc, how
can I enforce deallocation of the memory allocated during initialisation
? - i.e. at what point do I call Cleanup() ?
 
V

Victor Bazarov

Anonymous said:
I have a class (classA) that requires initialisation ONCE. It also
provides static methods that depend on various variables etc to have
been correctly initialised.

Some sample code to explain the scenario:

class ClassA
{
public:
~ClassA();
static ClassB * fetchB();
static ClassC * fetchC();

private:
ClassA();
static int dummy ;
};


//Impl
namespace
{
int MyInitStuf(){ return 42 ;} //assume this allocs memory
void Cleanup(); //used to clean up previously allocd mem
};

static int ClassA::dummy = MyInitStuf();


I have two questions re the code above:

1). Is the dummy variable GUARANTEED to have been initialised already
if I call one of the static methods (or are the satic variables first
initialised when an INSTANCE is created ?

No. What if you call one of the static member functions in another
static object's constructor? If the objects are in difference TU,
you have "static order initialisation fiasco", see FAQ. There is no
connection between creating an instance of a class and the class'
static data members, unless you create such a connection.
2). Supposing that the MyInitStuf() function allocates memory etc, how
can I enforce deallocation of the memory allocated during
initialisation ? - i.e. at what point do I call Cleanup() ?

Your 'dummy's destructor should probably do that. And since it's an int,
and you can't provide a destructor for it, you could have a companion
object that when destructed would access 'dummy' and free the memory.

V
 
J

Jonathan Lane

I have a class (classA) that requires initialisation ONCE. It also
provides static methods that depend on various variables etc to have
been correctly initialised.

Some sample code to explain the scenario:

class ClassA
{
public:
~ClassA();
static ClassB * fetchB();
static ClassC * fetchC();

private:
ClassA();
static int dummy ;

};

//Impl
namespace
{
int MyInitStuf(){ return 42 ;} //assume this allocs memory
void Cleanup(); //used to clean up previously allocd mem

};

static int ClassA::dummy = MyInitStuf();

I have two questions re the code above:

1). Is the dummy variable GUARANTEED to have been initialised already if
I call one of the static methods (or are the satic variables first
initialised when an INSTANCE is created ?

In general the order of initialisation of static variables is
undefined. In your case I think it depends. If you're calling it from
a non-static function you should be OK. If it's being called before
main - i.e. in the initialisation of another static variable then it's
undefined.
2). Supposing that the MyInitStuf() function allocates memory etc, how
can I enforce deallocation of the memory allocated during initialisation
? - i.e. at what point do I call Cleanup() ?

Is this an effort at the singleton pattern, or are you trying to
achieve something different? Generally, you should call cleanup when
you no longer want the memory to be allocated. I can't tell you when
that is for your program. Maybe try using a smart pointer to wrap this
class in so that Cleanup is called during it's deallocator?

Maybe you're trying to ask something different but take a look at
Singleton.
 
A

Anonymous

Victor said:
No. What if you call one of the static member functions in another
static object's constructor? If the objects are in difference TU,
you have "static order initialisation fiasco", see FAQ. There is no
connection between creating an instance of a class and the class'
static data members, unless you create such a connection.




Your 'dummy's destructor should probably do that. And since it's an int,
and you can't provide a destructor for it, you could have a companion
object that when destructed would access 'dummy' and free the memory.

V

This was going to be the next step forward - i.e. wrap up all the
initialisation paraphenalia and vaiables in a struct or class data type,
and declare/define a class member of that type in ClassA

However, according to wht you wrote earlier: "There is no connection
between creating an instance of a class and the class' static data
members, unless you create such a connection."

I'm not sure how even that (i.e. declaring dummy of type
'MyWrapperObject' gets around this unless - since mere invocation of the
static methods does not mean that the static variable has been defined -
hold on, but I am defining it at compile time - so maybe this is not an
issue? (I AM getting confused).
 
V

Victor Bazarov

Anonymous said:
This was going to be the next step forward - i.e. wrap up all the
initialisation paraphenalia and vaiables in a struct or class data
type, and declare/define a class member of that type in ClassA

However, according to wht you wrote earlier: "There is no connection
between creating an instance of a class and the class' static data
members, unless you create such a connection."

I'm not sure how even that (i.e. declaring dummy of type
'MyWrapperObject' gets around this unless - since mere invocation of
the static methods does not mean that the static variable has been
defined - hold on, but I am defining it at compile time - so maybe
this is not an issue? (I AM getting confused).

What you can do is make sure that during construction of an instance
of your class you call a function that would instantiate your "static
member". IOW, instead of relying on the static object to do your
initialisation of the static data member, try making your static
data member a static object inside some static function:

class HasStatic {
static StaticObjectType *pStatic;
static StaticObjectType &myStatic;
static int MakeSureStaticIsInitialised() {
static StaticObjectType actualObject;
HasStatic::pStatic = &actualObject;
}
public:
HasStatic() /* some initialisation here probably */ {
MakeSureStaticIsInitialised();
// whatever else you need to do
}
};

StaticObjectType *HasStatic::pStatic = 0;
int dummy = MakeSureStaticIsInitialised();
StaticObjectType &HasStatic::myStatic = *HasStatic::pStatic;

You're going to waste some cycles that way assigning the 'pStatic'
data member all the time when creating instances of 'HasStatic', but
at least there will be no doubt.

Please read the code carefully, not all of it is necessary, I think.
I just wanted to show how it's possible. Ask questions.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top