473,396 Members | 1,655 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,396 software developers and data experts.

singleton class problem

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 access and life is restricted within the thread, then a static
initialization is safe ?

Thanks
abir

Nov 30 '06 #1
6 3147
* toton:
Hi,
If I have a singleton class based on dynamic initialization (with new
) , is it considered a memory leak?
Generally the term "memory leak" refers to /repeated/ memory allocations
without corresponding deallocations, a worsening over time.

Anything in C++ standard says about it ?
No, it doesn't define "memory leak".

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?
Both, although the current C++ standard does not address threading issues.

However, take a look at Boost's thread local storage class.

If the singleton is per thread basis (then no more singleton though
), and access and life is restricted within the thread, then a static
initialization is safe ?
Define what you mean by "safe", best with some actual code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 30 '06 #2
toton wrote:
If I have a singleton class based on dynamic initialization (with new
) , is it considered a memory leak?
If you maintain a pointer to every dynamically allocated region of
memory, there is no leak. A leak occurs when you have allocated memory
or another resource and no longer have a pointer/handle to it.

I got this definition from one of Scott Meyers' books, and it seems
pretty good to me.

Nate
Nov 30 '06 #3

toton wrote:
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 ?
Yes, its a memory leak. What that entails is undefined. However, there
is no excuse for such a condition since a simple singleton using a
shared_ptr is the way to go. There are other benefits that some
shared_ptrs brings to the picture as well (ie: boost::shared_ptr). Read
some of the Posts here to find out what those are.
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?
All statics are affected by access if you use threads. That too has a
simple solution. write a class that locks / unlocks for you.
If the singleton is per thread basis (then no more singleton though
), and access and life is restricted within the thread, then a static
initialization is safe ?
Ask in an appropriate newsgroup that does threads.
Typically, static variables have internal linkage. In C++ thats
considered deprecated. A better alternative is to use statics in
namespaces or even in anonymous namespaces.

namespace whatever
{
static int n = 99;
static create() { ... }
}

Such a strategy is better design since a static variable named var in
one file is not the same static var in another. internal linkage =
static file linkage.

Nov 30 '06 #4
Hi,

If you are working with multithreading then Singleton implemention will
have problem if you are not using locking mechanism.Two or more thead
might be accessing Static function and crate more then one instance of
Singleton class. But locking will hit performance. So double check
locking is advised.
You can get more informaton ragarding doublechecked locking
http://www.cs.umd.edu/~pugh/java/mem...edLocking.html

Nov 30 '06 #5

Salt_Peter wrote:
toton wrote:
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 ?

Yes, its a memory leak.
Confused :(
Got two answer, one says yes the other no!
I always get two opposite answers for a question related to C++.
It is a memory leak from program's point of view. But doesn't OS cleans
the memory ?
What happens when I run new based singleton class several times. Will
the OS consume all
of the system memory (RAM) ?
confused again, and leaving the topic here (as, because people will say
it is off topic :( ).
>What that entails is undefined. However, there
is no excuse for such a condition since a simple singleton using a
shared_ptr is the way to go. There are other benefits that some
shared_ptrs brings to the picture as well (ie: boost::shared_ptr). Read
some of the Posts here to find out what those are.
Not sure how shared_ptr will be able to do it from outside. Searching
the newsgroup is not giving any hint.
So to have a second way, I have a near-singleton class. Which I know
standard enough so that no one creates a duplicate one (by convention,
not by design) . It is just a regular class and has a virtual
destruction, which gets called in program termination. In my case, it
is QApplication from Qt.
Now I have a few other "singleton class" by design. i.e they have
private ctor, copy ctor and copy assignment operator.
Now If I remove the singleton static pointer & get method, and make
them friend of QApplication (it is just a class in this context, not
off topic as it is a Qt class). and add them as member of QApplication.
Then I can delete them in usual way (with shared ptr or move_ptr or
even simple raw ptr) in QApplication destructor.
So, I don't have the problem of having multiple instance of those
"pseudo singleton classes", while only a single instance of
QApplication will keep track of it.
Anyone do this kind of thing (i.e clubbing several non-constructible,
non-copyable, non assignable classes, with a friend class, which has
the role of constructing & destructing them)
May be it is little similar to the Stroustrup's way of making a final
class.
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?

All statics are affected by access if you use threads. That too has a
simple solution. write a class that locks / unlocks for you.
here i have doubt about static linkage. What it actually refers ?
unlike object ,class members (i.e static members ) are one per class (i
or per class / per thread or something else). i.e for static only one
"memory location" (may not be an appropriate term, but hope conveys the
meaning) location per application ( or per thread ? ) If it is per
application, then not a problem for me, but otherwise yes. i.e my
requirement is, anywhere from the application &my_class::my_static_var
to return same value, irrespective of thread or anything else.
If the singleton is per thread basis (then no more singleton though
), and access and life is restricted within the thread, then a static
initialization is safe ?

Ask in an appropriate newsgroup that does threads.
Typically, static variables have internal linkage. In C++ thats
considered deprecated. A better alternative is to use statics in
namespaces or even in anonymous namespaces.

namespace whatever
{
static int n = 99;
static create() { ... }
}

Such a strategy is better design since a static variable named var in
one file is not the same static var in another. internal linkage =
static file linkage.
Nov 30 '06 #6
"toton" <ab*******@gmail.comwrote in
news:11**********************@h54g2000cwb.googlegr oups.com:
>
Salt_Peter wrote:
>toton wrote:
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 ?

Yes, its a memory leak.
Confused :(
Got two answer, one says yes the other no!
I always get two opposite answers for a question related to C++.
It is a memory leak from program's point of view. But doesn't OS
cleans the memory ?
What the OS does with the allocated memory after the program ends isn't
C++'s problem. So strictly speaking, I would say that you have a memory
leak in your program. Your object was not deleted by you. Perhaps
wrapping it into a smart pointer of some description (std::auto_ptr ?).
What happens when I run new based singleton class several times. Will
the OS consume all
of the system memory (RAM) ?
Huh? How do you "run new based singleton class serveral times"? If you
can have multiple instances of the object, it's not a singleton
anymore....

Nov 30 '06 #7

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

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared across several modules. I found the stuff on the...
16
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
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: 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: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
2
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates"...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
4
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.