473,407 Members | 2,598 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,407 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 3012
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
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
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,...
0
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...

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.