A
Anonymous
I want to carry out initialisation ONCE for use by a class.
I am using a dummy static variable dummy to do this. However, during my
initialisation code, I am allocating memory etc for various variables
used by the class.
Although I am not explicitly deallocating memory, alloc'd memory will be
returned to the heap when the app terminates - but I am not happy with
this. I am looking for a way to explicitly deallocate memory when there
are no more instances of the class that requires the new'd variables.
I can think of several ways of doing this - all of which seem a bit of a
hack (or maybe even wrong?). i'd like some comments on my ideas, and
pointers if any of my proposed solns are wrong or have side effects - or
preferably, a better way of solving this problem.
1). Use a class variable for reference counting and explicitly
"uninitialize" when last instance is dying
2). Wrap up all of the variables to be new'd during the initialisation
process, into a data type (struct or class), make a class variable of
that type, initialize it once, and deallocate in the new data types
destructor
Any more ideas ?
I am using a dummy static variable dummy to do this. However, during my
initialisation code, I am allocating memory etc for various variables
used by the class.
Although I am not explicitly deallocating memory, alloc'd memory will be
returned to the heap when the app terminates - but I am not happy with
this. I am looking for a way to explicitly deallocate memory when there
are no more instances of the class that requires the new'd variables.
I can think of several ways of doing this - all of which seem a bit of a
hack (or maybe even wrong?). i'd like some comments on my ideas, and
pointers if any of my proposed solns are wrong or have side effects - or
preferably, a better way of solving this problem.
1). Use a class variable for reference counting and explicitly
"uninitialize" when last instance is dying
2). Wrap up all of the variables to be new'd during the initialisation
process, into a data type (struct or class), make a class variable of
that type, initialize it once, and deallocate in the new data types
destructor
Any more ideas ?