Destructing global variables in correct order

J

Jarmo Muukka

Hi all!

I have a small problem I cannot find a good solution. The problem is
destructing global (static) variables in correct order. I try to mimic it in
here.

// file1.cpp contains implementation of Class1

static Class1 instance;

// static
void* Class1::alloc()
{
return ::instance.allocresource();
}

// static
void Class1::free( void* resource )
{
::instance.freeresource( resource );
}
--------------------------------------
// file2.cpp contains implementation of Class2

void Class2::foo()
{
this->Add( Class1::alloc() );
}

Class2::~Class2()
{
this->Clear(); // this calls Class1::free( resource ) in a loop
}
--------------------------------------
// file3.cpp
// this is an user file that uses Class2

static Class2 resources;
--------------------------------------
So, Class2 uses Class1 and both classes (instances) store data into their
own variables. Everything would be perfect if Class2 "resources" would be
destructed before Class1 "instance". Compiler just decides to destruct
"instance" first. It causes a problem because destructor of "resources"
needs "instance" (of course it has to tell to "instance" that it does not
need resources anymore).

More background: Class2 is used generally and it should be possible to be
global variable. Class1 is a global resource that Class2 needs. It is just
for implementation (user of Class2 should not be aware of it).

How would you solve the problem?

Thanks in advance,
JMu
 
F

flopbucket

Someone may correct me here, but in general, you can't decide the order
of destruction of global variables.

In this case here, can you put Class1 inside Class2?
 
T

Tomás

flopbucket posted:
Someone may correct me here, but in general, you can't decide the order
of destruction of global variables.


Maybe you can if they're defined in the same translation unit?

#include <string>

std::string a
std::string b;

int main() {}

"a" gets constructed first then "b".
"b" gets destructed first, then "a".

Not sure if that's how it works... might look it up.

-Tomás
 
J

Jarmo Muukka

Tomás said:
flopbucket posted:



Maybe you can if they're defined in the same translation unit?

#include <string>

std::string a
std::string b;

int main() {}

"a" gets constructed first then "b".
"b" gets destructed first, then "a".

Not sure if that's how it works... might look it up.

-Tomás

Objects cannot be defined in same translation unit. And, I cannot put Class1
inside Class2. It would take too much resources. And, I do not like global
variables. I try to avoid them as much as I can.

I solved the problem in a following way. Instead of static object, I have
static pointer to the object. This is reference counted. When there is a
first call to alloc in Class2, I create the object and each alloc increments
the reference count. Call to free will decrement the reference count and
when count drops to zero, I delete the object.

And of course the Class1 does not have static interface anymore. The object
has to be created to use the interface. Class2 does that (desciption above).

Thanks,
JMu
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top