473,756 Members | 4,256 Online
Bytes | Software Development & Data Engineering Community
+ 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.allo cresource();
}

// static
void Class1::free( void* resource )
{
::instance.free resource( 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 2126
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
4369
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 doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables - the best thing to do would be to use application variables - so long as there weren't too many...
2
3067
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 didn't write wxString g_appname = "Name"; I had to answer "I don't know", I just had the impression in global it
4
24184
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(); function loadUrlVariables() { varString = location.search;
8
2534
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 static variables) is initializated (constructed) before main is invoked . . ." .. . .
2
8832
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 { public: Book()
37
2747
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 order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
5
11826
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 $MYVAR, $a; ?>
1
29378
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
8
3825
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 called from any module with in an application. So far I have created a farily standard module that will act as my global config. This module will store variables that are populated from the database. So that I will not be forced to re-create/query...
0
9462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7259
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5155
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.