473,772 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Static" destructor

Hi,

I have a class A with a static pointer to some data. The pointer is static
because I want to access the data from all instances of the class. The data
itself is allocated when the first instance of A is constructed:

# a.h

class A
{
private:
static char* sm_data;

public:
A();
};

# a.cpp

A::A()
{
if (sm_data == NULL)
sm_data = new char[1024];
}

char* A::sm_data = NULL;

This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application? I could always use reference
counting and delete [] sm_data when the last instance of A is deleted, but
that is a little cumbersome. I could also have a static function that is
called from the application's main class when it exits, but then I'd have to
rely on other parts of the application.

My question is: how would you solve this?
Regards,
Jahn Otto

Jul 19 '05 #1
6 11737
Oops - typo. I wrote "I could always use reference counting[....]", but I
meant instance counting.
Jahn Otto
Jul 19 '05 #2
Jahn Otto Næsgaard Andersen wrote:
Hi,

I have a class A with a static pointer to some data. The pointer is static
because I want to access the data from all instances of the class. The data
itself is allocated when the first instance of A is constructed:

# a.h

class A
{
private:
static char* sm_data;

public:
A();
};

# a.cpp

A::A()
{
if (sm_data == NULL)
sm_data = new char[1024]; ^^^^^^^ no need for this
}

char* A::sm_data = NULL;
you could do this:

char* A::sm_data = new char[1024];

This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application?
You may not need to if this is a stan-alone application.

I could always use reference counting and delete [] sm_data when the last instance of A is deleted, but
that is a little cumbersome.
Use a helper class - in this case a std::string is great (and reccomended).

class A
{
private:
std::string sm_data;

public:
A();
};

# a.cpp

A::A()
{
}
ok - so if you don't like that because it breaks too much code here is a
bad bad bad hack. (not reccomended)

class A
{
private:
static char* sm_data;

public:
A();

private:
class Nuker
{
public:
~Nuker()
{
delete[] sm_data;
}

Nuker() {};

private:
Nuker( const Nuker & );
Nuker & operator = ( const Nuker & );

};

static Nuker a_nuker;
};

A::A()
{
Nuker & nuker = a_nuker;
}

char* A::sm_data = new char[1024];

A::Nuker A::a_nuker;
I could also have a static function that is called from the application's main class when it exits, but then I'd have to
rely on other parts of the application.

My question is: how would you solve this?


Jul 19 '05 #3


"Jahn Otto Næsgaard Andersen" wrote:

My question is: how would you solve this?


You are heading for a 'singleton'.
Try google to read more about it.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #4

"Jahn Otto Næsgaard Andersen" wrote:

[... lazy init stuff ...]
This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application?


Refcounting and thread-sharing aside for a moment***, try this:

http://groups.google.com/groups?selm...30AF5%40web.de
(Subject: Re: inheritance and singletons)

regards,
alexander.

***) http://opengroup.org/austin/mailarch.../msg04858.html
Jul 19 '05 #5

"Jahn Otto Næsgaard Andersen" <sp*******@jott o.no> wrote in message
news:Kx******** *************@j uliett.dax.net. ..


This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application?


Why don't you just delete it if it's not zero, and then set it to zero?
Jul 19 '05 #6
Jahn Otto Næsgaard Andersen wrote:
Hi,

I have a class A with a static pointer to some data. The pointer is static
because I want to access the data from all instances of the class. The data
itself is allocated when the first instance of A is constructed:

# a.h

class A
{
private:
static char* sm_data;


If you can replace this with

static std::string sm_data;

then do so. It will save you headaches, and solve your immediate problem.

Another alternative is to use a smart pointer here. Something similar to
std::auto_ptr should work, but auto_ptr itself is not appropriate,
because it will use 'delete' rather than 'delete[]'. You'd have to
define your own 'auto_array' or use one from an existing library.

Using a smart pointer would let the rest of the code remain basically
untouched, so it's a good "quick fix". However, quick isn't always the
best way. Your overall design would probably benefit from switching to
something that allocates and manages memory for you, such as the
std::string suggested above.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

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

Similar topics

29
4413
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
3
12370
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
12
13471
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one of your Derived classes will need to implement some method, but implement it differently, and that the base class cannot implement it. This is where pure virtual comes in.
3
1385
by: ruud.bos | last post by:
Hi list, As a C++ newbie, I have a question about static member functions. Suppose I have the following class definition: class MyClass { public: static void MyFunc(); };
9
2310
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
11
2159
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false) { if (is_dir("$baseDir/$fyl")) blah($item);
9
2070
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
3
5859
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ section describes a solution using methods returning references to static objects. But consider:
14
6027
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
9621
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
10106
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...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
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
7461
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
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
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
5484
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.