473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Re: destroy singleton?

On Apr 11, 12:09 am, Ron Eggler <t...@example.comwrote:
I created a singleton class and can't get rid of it anymore ;)
That's usually a feature, not a bug, where singletons are
concerned. In general, I avoid ever deleting a singleton.
Uhm, the important things in the definition look like that:
[header]
class GPIOcontrol: public TSPThread
{
public:
static GPIOcontrol* instance(GPSData*);
// Method returns pointer of singleton
// instance of this class
~GPIOcontrol();
static GPIOcontrol* pinstance;
// The instance pointer that is returned
by the method instance() - this is a singleton class

protected:
GPIOcontrol(GPSData *gpsDataObj);

private:
};
void PrepareToDie(void);
// This method unsets the singleton pointer
[/header]
I call the PrepareToDie() function from the destructor and get
a segmentation fault, the implementation of this function
looks like:
From the destructor of what?
[implementation]
void PrepareToDie(void)
//This method unsets the singleton pointer
{
delete GPIOcontrol::pinstance;
}
[/implementation]
I first had thsi delete PrepareToDie function as a public
method in the class what returned me a seg. fault so i thought
i could resolve it by moving it out of the class but it
apparently wouldn't work. Does anyone know how i can get rid
of it and free its memory?
Most of the time, it is preferable not to delete the singleton,
ever. That way, it remains accessible even in destructors of
static objects. In the rare cases where deletion is necessary,
the usual solution I've seen is to use a local static:

GPIOcontrol*
GPIOcontrol::instance()
{
static GPIOcontrol theOneAndOnly ;
return theOneAndOnly ;
}

In this case, the compiler takes care of ensuring that the
object is only created once, and that the destructor is called
once during program shutdown.

(Note that this will only work in a multithreaded environment if
you can ensure that instance is called at least once before
threads are started, or that it is only called from a single
thread. Otherwise, you need a scoped lock before the static.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #1
1 4136
If you don't need thread safety then a very nice (and easy) way to create
a singleton is by using the Myers singleton pattern...

http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B

.... Which is what James has just said.
Jun 27 '08 #2

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

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: Andy Champ | last post by:
Ron Eggler wrote: <snip> I assume you do null the pointer after deleting it? And you are single threaded? Andy
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.