472,958 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

singleton template

Hi,

I found this in code I was maintaining

template <class SingletonClass>
SingletonClass* Singleton<SingletonClass>::instance ()
{
static SingletonClass _instance;
return &_instance;
}

there are indications that the constructor is getting called more than
once
in some circumstances (tracing in one of the instantiations of the
template).

Are there potential problems with the use of static data?
--
Nick Keighley

Jul 3 '06 #1
15 2989
Nick Keighley wrote:
Hi,

I found this in code I was maintaining

template <class SingletonClass>
SingletonClass* Singleton<SingletonClass>::instance ()
{
static SingletonClass _instance;
return &_instance;
}

there are indications that the constructor is getting called more than
once
in some circumstances (tracing in one of the instantiations of the
template).

Are there potential problems with the use of static data?
There are sometimes, but there's nothing inherently wrong with this
code (you might consider using references instead, however). See
chapter 6 of _Modern C++ Design_ for more than you ever wanted to know
about singletons in C++.

Cheers! --M

Jul 3 '06 #2
Hi Nick,
there are indications that the constructor is getting called more
than once in some circumstances (tracing in one of the
instantiations of the template).

Are there potential problems with the use of static data?
I had exactly this problem when using Visual C++ 6.0. But only
in Release mode, not in Debug mode. Perhaps a problem with
the optimizer...?!

The solution was to move the implementation of the instance()
method from the .h to the .cpp file.

Hope it helps...
Tilman

Jul 3 '06 #3

mlimber wrote:
Nick Keighley wrote:
Hi,

I found this in code I was maintaining

template <class SingletonClass>
SingletonClass* Singleton<SingletonClass>::instance ()
{
static SingletonClass _instance;
return &_instance;
}

there are indications that the constructor is getting called more than
once
in some circumstances (tracing in one of the instantiations of the
template).

Are there potential problems with the use of static data?

There are sometimes, but there's nothing inherently wrong with this
code (you might consider using references instead, however). See
chapter 6 of _Modern C++ Design_ for more than you ever wanted to know
about singletons in C++
..
thanks, I keep on meaning to getting around to MCD, but I am not
comfortable with templates.

For instance something like this:-

class PerformanceDataItemIniFile : public PanelIniFile,

public Singleton< PerformanceDataItemIniFile >
...

worries me it inherits from something that uses itself as a parameter.

--
Nick Keighley

Jul 3 '06 #4
"Nick Keighley" <ni******************@hotmail.comwrote in message
news:11**********************@b68g2000cwa.googlegr oups.com
Hi,

I found this in code I was maintaining

template <class SingletonClass>
SingletonClass* Singleton<SingletonClass>::instance ()
{
static SingletonClass _instance;
return &_instance;
}

there are indications that the constructor is getting called more than
once
in some circumstances (tracing in one of the instantiations of the
template).

Are there potential problems with the use of static data?

Compiler bugs aside, I think this could only be a problem with a
multi-threaded application.

http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx
--
John Carson
Jul 3 '06 #5
Nick Keighley wrote:
mlimber wrote:
Nick Keighley wrote:
Hi,
>
I found this in code I was maintaining
>
template <class SingletonClass>
SingletonClass* Singleton<SingletonClass>::instance ()
{
static SingletonClass _instance;
return &_instance;
}
>
there are indications that the constructor is getting called more than
once
in some circumstances (tracing in one of the instantiations of the
template).
>
Are there potential problems with the use of static data?
There are sometimes, but there's nothing inherently wrong with this
code (you might consider using references instead, however). See
chapter 6 of _Modern C++ Design_ for more than you ever wanted to know
about singletons in C++
.
thanks, I keep on meaning to getting around to MCD, but I am not
comfortable with templates.
You might want to try to look through at least chapter 6 because he
discusses some of the pitfalls with singletons in C++ (including in
multithreading, longevity, etc.) and ways to handle them.
For instance something like this:-

class PerformanceDataItemIniFile : public PanelIniFile,

public Singleton< PerformanceDataItemIniFile >
...

worries me it inherits from something that uses itself as a parameter.
That's the Curiously Recurring Template Pattern (see, e.g.,
http://en.wikipedia.org/wiki/Curious...mplate_Pattern) and
can be quite useful. However, with singletons, I would expect to see
something more like this for your template class:

template<class T>
class Singleton
{
public:
static T& Instance();
private:
// Disabled functions
Singleton();
Singleton( const Singleton& );
Singleton& operator=( const Singleton& );
Singleton* operator&();
~Singleton();
};

template<class T>
T& Singleton<T>::Instance()
{
static T myObject;
return myObject;
}

Which is not inherited from but used as a wrapper like this:

class A
{
private:
// Private constructor/destructor disallows creation
// except by friends.
friend class Singleton<A>;
A();
~A();

// Disabled functions for singleton usage
A( const A& );
A& operator=( const A& );
A* operator&();

public:
void DoSomething();
// ...
};

Singleton<AtheA;

void Foo()
{
theA::Instance().DoSomething();
}

Cheers! --M

Jul 3 '06 #6

Tilman Kuepper wrote:
Hi Nick,
there are indications that the constructor is getting called more
than once in some circumstances (tracing in one of the
instantiations of the template).

Are there potential problems with the use of static data?

I had exactly this problem when using Visual C++ 6.0. But only
in Release mode, not in Debug mode. Perhaps a problem with
the optimizer...?!

The solution was to move the implementation of the instance()
method from the .h to the .cpp file.

Hope it helps...
Tilman
Jul 3 '06 #7

Tilman Kuepper wrote:
Hi Nick,
there are indications that the constructor is getting called more
than once in some circumstances (tracing in one of the
instantiations of the template).

Are there potential problems with the use of static data?

I had exactly this problem when using Visual C++ 6.0. But only
in Release mode, not in Debug mode. Perhaps a problem with
the optimizer...?!

The solution was to move the implementation of the instance()
method from the .h to the .cpp file.
oops posted message with no additional text!

by a strange coincidence I'm using VCC 5 and the fault only occurs in
the
Release version...
--
Nick Keighley

Programming should never be boring, because anything
mundane and repetitive should be done by the computer.
~Alan Turing

Jul 3 '06 #8
Tilman Kuepper wrote:
Hi Nick,
there are indications that the constructor is getting called more
than once in some circumstances (tracing in one of the
instantiations of the template).

Are there potential problems with the use of static data?

I had exactly this problem when using Visual C++ 6.0. But only
in Release mode, not in Debug mode. Perhaps a problem with
the optimizer...?!
Possible but it seems more likely that this is symptomatic of other
latent problems. See "Surviving the Release Version" by Joseph M.
Newcomer:

http://www.flounder.com/debug_release.htm

Particularly, this section:

http://www.flounder.com/debug_releas...0Bugs%20(again)
The solution was to move the implementation of the instance()
method from the .h to the .cpp file.
That's generally a bad idea. Absent the export keyword, for
maintainability the function should be in the header. Duplicating it by
hand in every file that needs it is a maintenance nightmare. (I should
note that one of my compilers complained if I defined the Instance()
function given elsewhere in this thread inline in the class definition,
but moving it outside the class definition but still in the header file
quelled the complaint but kept maintenance the same.)

Cheers! --M

Jul 3 '06 #9
Tilman Kuepper wrote:
Hi Nick,
oops posted message with no additional text!

there are indications that the constructor is getting called more
than once in some circumstances (tracing in one of the
instantiations of the template).

Are there potential problems with the use of static data?

I had exactly this problem when using Visual C++ 6.0. But only
in Release mode, not in Debug mode. Perhaps a problem with
the optimizer...?!
by a strange coincidence I'm using VCC 5 and the fault only occurs in
the
Release version...

The solution was to move the implementation of the instance()
method from the .h to the .cpp file.
err, it's a template- doesn't it all have to go in the header file?
--
Nick Keighley

Programming should never be boring, because anything
mundane and repetitive should be done by the computer.
~Alan Turing

Jul 3 '06 #10
"Nick Keighley" <ni******************@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com
Tilman Kuepper wrote:
>Hi Nick,

I had exactly this problem when using Visual C++ 6.0. But only
in Release mode, not in Debug mode. Perhaps a problem with
the optimizer...?!

by a strange coincidence I'm using VCC 5 and the fault only occurs in
the
Release version...

>The solution was to move the implementation of the instance()
method from the .h to the .cpp file.

err, it's a template- doesn't it all have to go in the header file?
Template definitions need to be visible to the compiler in the translation
unit in which they are used. Putting them in the header file is the easiest
way to ensure this. However, if, for example, a function is only called in
file.cpp, then there is no problem if you define the function in file.cpp.
--
John Carson
Jul 3 '06 #11
John Carson posted:

Template definitions need to be visible to the compiler in the
translation unit in which they are used.

Unless of course we use "export"...
--

Frederick Gotham
Jul 3 '06 #12
Hi Nick,
>The solution was to move the implementation of the instance()
method from the .h to the .cpp file.

err, it's a template- doesn't it all have to go in the header file?
In my case it was without templates, so I had no problems to
remove the implementation from the header file. Nevertheless
it may be possible that we both found the same problem in the
compiler/optimizer.

Perhaps you can replace the template by separate classes
for the different Singletons?

You might also try to move the template implementation to
a .cpp file using explicit instantiation.

Good luck,
Tilman

Jul 4 '06 #13
Tilman Kuepper wrote:
The solution was to move the implementation of the instance()
method from the .h to the .cpp file.
err, it's a template- doesn't it all have to go in the header file?

In my case it was without templates, so I had no problems to
remove the implementation from the header file. Nevertheless
it may be possible that we both found the same problem in the
compiler/optimizer.
I suspect you might be right
Perhaps you can replace the template by separate classes
for the different Singletons?
there are only 324 instantiations of the template...
You might also try to move the template implementation to
a .cpp file using explicit instantiation.

Good luck,
one of the websites I was pointed out discusses the problems
of Release/Debug. Turning off optimisation may be the easiest fix
(the bug is showing up on the client/gui side of the system- so
it is probably less in need of optimisation)
--
Nick Keighley

Jul 4 '06 #14
Frederick Gotham wrote:
John Carson posted:
Template definitions need to be visible to the compiler in the
translation unit in which they are used.


Unless of course we use "export"...
I have the strangest feeling I'd have a problem with "export"...

What is support like for "export" on current compilers? (I'm not
using a current compiler, yes I know)
--
Nick Keighley

Jul 4 '06 #15
Nick Keighley <ni******************@hotmail.comwrote:
What is support like for "export" on current compilers? (I'm not
using a current compiler, yes I know)
As far as I know, only the Comeau compiler officially supports it, and I
think the Intel compiler supports it as a hidden feature (it may need a
special command-line option or something).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 11 '06 #16

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

Similar topics

2
by: Sean Dettrick | last post by:
Hi, apologies for yet another Singleton posting. From reading around previous postings etc, I've cobbled together two Singleton template definitions - one that returns a reference, and one that...
7
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)...
2
by: Chr?stian Rousselle | last post by:
Hello, I want to do derive a class from a Singleton template base class. Is there a way to solve the following problem: template<class T> class CSingleton { public: static T&...
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 ...
5
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++...
0
by: MechSoft | last post by:
Hi, there, I have an issue about globally access data. My multi-threaded application has some resources that need to be accessed by multiple threads and have the lifetime of the application,...
2
by: keepyourstupidspam | last post by:
Hi, I am wondering if My Singleton class is ok from a thread safety point of view. Am i adding the thread locks in the right places? template <typename T> class DLLEXPORT CSingleton
2
by: puzzlecracker | last post by:
Will this function is singleton or do I need to declare a ctor in the derived class as public, as well as copy-ctor and assignment operator. template<typename T> class CSingleton { public:...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.