473,763 Members | 3,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton destruction

Hello all,

I have made a class into a singleton by having a static get() method which
returns a pointer to the one-and-only instance. This method creates the
instance on the heap if it hasn't already done so in a prior call.
Constructors are private so that the class may not be instaniated from
outside. As I understand it, this is the standard approach and is nothing
new.

I have no need to ever destroy this singleton and re-create a new one in
it's place, so I have not provided a method for destruction.

My question is: Where in my code, if ever, should the singleton be
deallocated? Is it standard practice to just ignore this memory, or would
it really be more proper to find some way to deallocate it manually upon
app. exit?

Thanks,
Dave
Jul 22 '05 #1
4 4194

"Dave" <be***********@ yahoo.com> wrote in message
news:10******** *****@news.supe rnews.com...
Hello all,

I have made a class into a singleton by having a static get() method which
returns a pointer to the one-and-only instance. This method creates the
instance on the heap if it hasn't already done so in a prior call.
Constructors are private so that the class may not be instaniated from
outside. As I understand it, this is the standard approach and is nothing
new.
There is another approach, declare the object as static within the get
method. This ensures that it will be destructed on exit.

class X
{
static X& get()
{
static X the_x;
return the_x;
}
};

I have no need to ever destroy this singleton and re-create a new one in
it's place, so I have not provided a method for destruction.

My question is: Where in my code, if ever, should the singleton be
deallocated? Is it standard practice to just ignore this memory, or would
it really be more proper to find some way to deallocate it manually upon
app. exit?
Depends on how good your OS is on reclaiming resources from exited
applications. I don't think operating systems have much problem with memory,
that will automatically be reclaimed. Other resources, well I think you have
to consult your documentation.

The best discussion I know of singletons and the policies you can adopt for
them is in Andrei Alexandrescu's book Modern C++ Design. You can get the
code if not the book online, google for loki library.

john

Thanks,
Dave

Jul 22 '05 #2

"Dave" <be***********@ yahoo.com> wrote in message
news:10******** *****@news.supe rnews.com...
Hello all,

I have made a class into a singleton by having a static get() method which
returns a pointer to the one-and-only instance. This method creates the
instance on the heap if it hasn't already done so in a prior call.
Constructors are private so that the class may not be instaniated from
outside. As I understand it, this is the standard approach and is nothing
new.
There is another approach, declare the object as static within the get
method. This ensures that it will be destructed on exit.

class X
{
static X& get()
{
static X the_x;
return the_x;
}
};

I have no need to ever destroy this singleton and re-create a new one in
it's place, so I have not provided a method for destruction.

My question is: Where in my code, if ever, should the singleton be
deallocated? Is it standard practice to just ignore this memory, or would
it really be more proper to find some way to deallocate it manually upon
app. exit?
Depends on how good your OS is on reclaiming resources from exited
applications. I don't think operating systems have much problem with memory,
that will automatically be reclaimed. Other resources, well I think you have
to consult your documentation.

The best discussion I know of singletons and the policies you can adopt for
them is in Andrei Alexandrescu's book Modern C++ Design. You can get the
code if not the book online, google for loki library.

john

Thanks,
Dave

Jul 22 '05 #3
On Thu, 8 Apr 2004 19:30:52 +0100 in comp.lang.c++, "John Harrison"
<jo************ *@hotmail.com> wrote,
There is another approach, declare the object as static within the get
method. This ensures that it will be destructed on exit.


And if something requires that the object be allocated with new, that's
a case where a static std::auto_ptr<t > can do the job.

Jul 22 '05 #4
On Thu, 8 Apr 2004 19:30:52 +0100 in comp.lang.c++, "John Harrison"
<jo************ *@hotmail.com> wrote,
There is another approach, declare the object as static within the get
method. This ensures that it will be destructed on exit.


And if something requires that the object be allocated with new, that's
a case where a static std::auto_ptr<t > can do the job.

Jul 22 '05 #5

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

Similar topics

4
2889
by: Damon | last post by:
Hi, I tried to perform a singleton pattern with lazy instantiation but keep getting linker errors. Could someone advise what I'm doing wrongly? I'm using gcc version 3.2.2. Thanks in advance. --------------linker errors------------- eve.o(.text+0x366): In function `main': /usr/include/c++/3.2.2/i386-slackware-linux/bits/atomicity.h:50: undefined reference to `eve::sq_note::sq_note()'
4
575
by: Dave | last post by:
Hello all, I have made a class into a singleton by having a static get() method which returns a pointer to the one-and-only instance. This method creates the instance on the heap if it hasn't already done so in a prior call. Constructors are private so that the class may not be instaniated from outside. As I understand it, this is the standard approach and is nothing new. I have no need to ever destroy this singleton and re-create a...
3
15091
by: JSheble | last post by:
I understand the difference between the two types (Singleton & SingleCall) but what affect does this have if the Remoting Server accepts multiple connections from multiple clients, and they're all sending messages or using methods? Does it (server) manage this automatically?
11
3181
by: tomgee | last post by:
I saw it many times, // T.h: class T { public: static T* instance(); private: T() {}
6
3197
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the singleton is initialized as a static variable , it seems there is some threading issue . Is it the issue during singleton initialization only , or during the access also? If the singleton is per thread basis (then no more singleton though ), and...
10
3327
by: wkaras | last post by:
Why aren't "Singletons" done like this in C++: A.hpp: class A { public: static A theA; // ...
2
1929
by: EvilOldGit | last post by:
In the stuff I've read about multi threaded singleton it seems to assume that creating the Lock is atomic, and various sources, including Sutter go for RAII. { Lock l; if ( ! inst ).... } But I don't see how to do this with a real O/S API. The ones I've looked at have a two step process to making a lock, they require a "Create" function and a "Start Locking" function.
24
2573
by: Eric | last post by:
I created a singleton class as in the example below. The application sporadically crashes in the second line of the main function as shown. However, when I change the singleton such that the static pointer is a class member (defined in the cpp file) and the instance function creates the object if the pointer is NULL, then it works fine. I would appreciate any explanations as to why this happens. class CTestClass { public:
6
3374
by: Stephen Torri | last post by:
I am trying to produce a singleton class that I can use throughout my library to write tracing information to a file. My intent was to design such that someone using the library in its debug mode would be able to see what was happening without having to use a debugger to step through each instruction. What they would do is run their program and view the tracing file output. If there was something wrong then they would use the debugger of...
3
18252
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
9563
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
9386
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
9998
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
7366
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
6642
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
5270
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...
1
3917
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
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.