473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Destructing global variables in correct order

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
May 16 '06 #1
3 2109
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?

May 16 '06 #2
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
May 16 '06 #3
"Tomás" <NU**@NULL.NULL> wrote in message
news:hK******************@news.indigo.ie...
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


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
May 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
2
by: Otto Wyss | last post by:
I have a string variable g_appname which should be global accessable by any class who whishes. I have defined it as wxString *g_appname = NULL; and fills it later on. Now a friend asked why I...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
8
by: rottmanj | last post by:
. In order to teach my self more. I have started to convert some of my cf scheduled tasks to perl applications. One area where things are kind of fuzzy is setting up global variables that can be...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.