I have a singleton class that looks a little like this:
class MyClass
{
private:
//data
MyClass()
{
Create();
}
void Create(); //initialization stuff
public:
static MyClass* Instance()
{
static MyClass instance;
return &instance;
}
//interface
};
This singleton class is used within multiple cpp files within my
project. It works fine in debug build but when I compile a release
build the MyClass ctor is called multiple times, one time for each
different cpp file it is called from.
Do you know why this is happening? I don't understand what's going on
at all. Surely it is only possible for the ctor to be called once
given this code.
Thanks for any enlightenment. 9 4776
Hi,
if you are defining your static Instance() method inside a header file, this
should result in an behaviour as you described. Because you have more than
only one static instance of your singleton class.
If you are defining the Instance() method outside of the class in a cpp
file, it should work correctly.
Ralf www.cplusplus-kurse.de
"tarmat" <ta****@btopenw orld.com> schrieb im Newsbeitrag
news:43******** *************** *********@4ax.c om... I have a singleton class that looks a little like this:
class MyClass { private:
//data
MyClass() { Create(); }
void Create(); //initialization stuff
public:
static MyClass* Instance() { static MyClass instance;
return &instance; }
//interface };
This singleton class is used within multiple cpp files within my project. It works fine in debug build but when I compile a release build the MyClass ctor is called multiple times, one time for each different cpp file it is called from.
Do you know why this is happening? I don't understand what's going on at all. Surely it is only possible for the ctor to be called once given this code.
Thanks for any enlightenment.
tarmat wrote: I have a singleton class that looks a little like this:
class MyClass { private:
//data
MyClass() { Create(); }
void Create(); //initialization stuff
public:
static MyClass* Instance() { static MyClass instance;
return &instance; }
//interface };
This singleton class is used within multiple cpp files within my project. It works fine in debug build but when I compile a release build the MyClass ctor is called multiple times, one time for each different cpp file it is called from.
Do you know why this is happening? I don't understand what's going on at all. Surely it is only possible for the ctor to be called once given this code.
Thanks for any enlightenment.
If you move the definition of MyClass::Instan ce() into a '.cpp' file, and
that works, then the problem is that your compiler doesn't make static data
defined in inline member functions refer to the same item; I only know this
through similar experience with two different compilers. On older compilers,
if static class data is defined in headers, then every module that includes
that header gets its own unique static data; newer compilers ensure that
there is only one instance if the data.
Tim
thanks guys, I didn't know that
tarmat wrote: static MyClass* Instance() { static MyClass instance;
return &instance; }
IMO a pointer is the wrong thing to return there. Passing a non-const
pointer often implies to the client that the caller owns the returned
pointer. Passing a reference gives the clear message that the object is not
to be deleted by clients.
This singleton class is used within multiple cpp files within my project. It works fine in debug build but when I compile a release build the MyClass ctor is called multiple times, one time for each different cpp file it is called from.
Remove the 'static' part if you compile this inline. (i learned this lesson
only a few weeks ago, in a case almost identical to yours.)
--
----- stephan beal http://s11.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
stephan beal wrote: tarmat wrote: static MyClass* Instance() { static MyClass instance;
return &instance; }
IMO a pointer is the wrong thing to return there. Passing a non-const pointer often implies to the client that the caller owns the returned pointer. Passing a reference gives the clear message that the object is not to be deleted by clients.
This singleton class is used within multiple cpp files within my project. It works fine in debug build but when I compile a release build the MyClass ctor is called multiple times, one time for each different cpp file it is called from.
Remove the 'static' part if you compile this inline. (i learned this lesson only a few weeks ago, in a case almost identical to yours.)
Stephan,
Hi. The poster can't simply remove 'static'; to do so would mean that
instance is a temporary object and Instance() would be returning a pointer
to that temporary object. Also the singleton would be constructed and
destructed every time someone tried to get a handle to it... which is
probably not what's wanted.
The options are:
1) move MyClass::Instan ce() to a '.cpp' file
2) move 'instance' out of Instance() and into MyClass (keeping it static) an
define it in a '.cpp' file
3) find one of these trendy compilers that knows what to do
Personally, I like 3) since it gets rid of the need for '.cpp' files that
contain one function :-)
Tim
"tarmat" <ta****@btopenw orld.com> wrote in message
news:43******** *************** *********@4ax.c om... I have a singleton class that looks a little like this:
class MyClass { private:
//data
MyClass() { Create(); }
void Create(); //initialization stuff
public:
static MyClass* Instance() { static MyClass instance;
return &instance; }
//interface };
This singleton class is used within multiple cpp files within my project. It works fine in debug build but when I compile a release build the MyClass ctor is called multiple times, one time for each different cpp file it is called from.
Do you know why this is happening? I don't understand what's going on at all. Surely it is only possible for the ctor to be called once given this code.
No, I don't think this is a standard approach. You're creating a static
instance in your *header* file. You want one instance per application, not
one per inclusion of header file. (By the way, just because you have a
singleton class doesn't necessarily mean you *have* to have an instance.
Are you sure you don't want to create that instance somewhere else, if and
when you need it?)
Tim Clacy wrote: Hi. The poster can't simply remove 'static'; to do so would mean that instance is a temporary object and Instance() would be returning a pointer to that temporary object. Also the singleton would be constructed and
Sorry, you misunderstood (and i was ambiguous): i meant the static qualifier
from the function, not the internal static variable.
--
----- stephan beal http://s11n.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
stephan beal wrote: Tim Clacy wrote: Hi. The poster can't simply remove 'static'; to do so would mean that instance is a temporary object and Instance() would be returning a pointer to that temporary object. Also the singleton would be constructed and
Sorry, you misunderstood (and i was ambiguous): i meant the static qualifier from the function, not the internal static variable.
Stephan,
....but if you remove the static qualifier from 'Instance()', you will need
an instance of the class to get to the the 'Instance()' member function;
it's not a singleton anymore.
Tim
Tim Clacy wrote: Stephan,
...but if you remove the static qualifier from 'Instance()', you will need an instance of the class to get to the the 'Instance()' member function; it's not a singleton anymore.
Oh, doh.
/slap forehead.
--
----- stephan beal http://s11n.net/
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.
* This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Richard A. DeVenezia |
last post by:
foo() generates elements with event handlers that invoke foo function
properties.
Is this an abhorrent or misthought pattern ?
It allows just the one occurence of identifier /foo/ to be changed to
/whatever/ when need arises and everything should still work.
function foo () {
var callee = arguments.callee
|
by: Eric |
last post by:
Perhaps this question has been posed before (I'd be surprised if it
hasn't) but I just gotta know...
Is it possible to combine the Singleton and Factory Method design
patterns in the same class?
Let's start with your basic Singleton class:
class Singleton
{
|
by: ferdinand.stefanus |
last post by:
Hi
Could someone tell me what's the difference between these two singleton
implementations:
First implementation:
class Singleton
{
public:
|
by: Stephen Brown |
last post by:
I posted a question yesterday about a Singleton I have, using the standard
dotNet pattern, that seems to get recreated several times a day. I turned
on my performance counters (thanks to Mickey William's post yesterday) and
my Singleton restarting happens at the same time as an application restart.
Now it's pretty clear it has nothing to do with garbage collection and it
makes sense that application restart would destroy my singleton. ...
|
by: Sharon |
last post by:
I wish to build a framework for our developers that will include a singleton
pattern.
But it can not be a base class because it has a private constructor and
therefore can be inherit.
I thought maybe a Template can be use for that, but C# does not support
Templates (will be C# generics in mid 2005).
Does anyone have a solution on how the singleton pattern can be written, in
C#, as a framework/ infrastructure class, so users can use this...
| |
by: Paul Bromley |
last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I
need to use this to have one instance of my Class running and I think I
understand how to do this. My question however is can a singleton class have
a number of paramterised constructors enabling me to pass in parameters or
not?
I am trying to use the following to send in a parmeter to a constructor, but
getting an error with it. I have a feeling that I am not...
|
by: Kevin Newman |
last post by:
I have been playing around with a couple of ways to add inheritance to a
JavaScript singleton pattern. As far as I'm aware, using an anonymous
constructor to create a singleton does not allow any kind of inheritance:
singletonObj = new function() {
this.prop = true;
}
Here are two ways to create a singleton with inheritance:
|
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...
|
by: wizwx |
last post by:
There are two typical implementations of a singleton. The first one is
to use a static pointer
class Singleton {
static Singleton * pSingleton;
public:
Singleton * instance() {
if(pSingleton==NULL)
pSingleton = new Singleton;
return pSingleton;
}
|
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" I mean classes for which
terrible problems would clearly arise if more than one instance were to
exist. But as I'm getting into the design of this new solution, I'm
realizing that a large percentage of the classes _could be_ implemented as...
|
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...
| |
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |