"Static" destructor

  • Thread starter Jahn Otto Næsgaard Andersen
  • Start date
J

Jahn Otto Næsgaard Andersen

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
 
J

Jahn Otto Næsgaard Andersen

Oops - typo. I wrote "I could always use reference counting[....]", but I
meant instance counting.


Jahn Otto
 
G

Gianni Mariani

Jahn said:
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
 
K

Karl Heinz Buchegger

Jahn Otto Næsgaard Andersen said:
My question is: how would you solve this?

You are heading for a 'singleton'.
Try google to read more about it.
 
J

jeffc

Jahn Otto Næsgaard Andersen said:
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?
 
K

Kevin Goodsell

Jahn said:
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
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top