473,769 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: destroy singleton?

On Apr 11, 12:09 am, Ron Eggler <t...@example.c omwrote:
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(GPSDat a*);
// 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(GPS Data *gpsDataObj);

private:
};
void PrepareToDie(vo id);
// 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(vo id)
//This method unsets the singleton pointer
{
delete GPIOcontrol::pi nstance;
}
[/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::in stance()
{
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 objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #1
1 4168
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
12479
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) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
10
2654
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
2453
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 template <class T> class Singleton : public T { public: static T * Instance();
3
2498
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 what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
7
3289
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 avoid it? Purify does not show it has mem leak. // test if singleto class has mem leakage #include <iostream>
3
3926
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
5310
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++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
3
18253
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
0
191
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
9579
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
9416
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
8861
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
7393
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
6661
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
5293
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
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3948
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
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.