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 4 4164
"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.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
"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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...
|
by: tomgee |
last post by:
I saw it many times,
// T.h:
class T
{
public:
static T* instance();
private:
T() {}
|
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...
|
by: wkaras |
last post by:
Why aren't "Singletons" done like this in C++:
A.hpp:
class A
{
public: static A theA;
// ...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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...
| |