Jump to content
  • Advertisement
Sign in to follow this  

Singleton Question

This topic is 4132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi there i write a simple singleton template, its simple because i dont need it to be re-born-able or its life-time to be controlled in run-time. it works very well in debug mode, but crashed in release mode so i have to make a little change to make it work in release mode as well. although it works now, but i still wonder why its first version crashed in release mode, and is there still any crash risk in second implementation, here is the code :
// first version, crashed in Release mode
template <class T>
class CSingletonT
{
    CSingletonT(const CSingletonT &);
    CSingletonT &operator=(const CSingletonT &);

public :
    static T &data()
    {
        static T instance;
        return instance;
    }
};

// second version, works well in both debug and release mode
template <class T>
class CSingletonT
{
    CSingletonT(const CSingletonT &);
    CSingletonT &operator=(const CSingletonT &);

public :
    static T &data() 
    { 
        static T *instance = 0;
        if (!instance)
            instance = new T;

        return *instance;
    }
};

thanks for your suggestion and any hints

Share this post


Link to post
Share on other sites
Advertisement
People will tell you that singletons are the tool of the devil, but everything has a place. They key is not to overuse them. Some projects that are lauded as fine examples of OOP, like Ogre3D, use singletons.

Singleton.h

#pragma once

namespace SomeNamespace
{
template<typename T>
class Singleton
{
protected:
static T * _singleton;
public:
Singleton()
{
_singleton = static_cast< T* >( this );
}

static T * GetPointer()
{
if(_singleton == NULL)
_singleton = new T();
return _singleton;
}
};
}


Singleton.cpp


#include "stdafx.h"

#include "Singleton.h"

namespace SomeNamespace
{
template <typename T> T* Singleton <T>::_singleton = NULL;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by gharen
Some projects that are lauded as fine examples of OOP, like Ogre3D, use singletons.
Ogre is only lauded as a fine example by the hoards of coders who don't understand OOP. They see a ton of classes and simple design patterns like singletons, so they decide it must be really good object oriented code.

Share this post


Link to post
Share on other sites
Quote:
Original post by gharen
People will tell you that singletons are the tool of the devil, but everything has a place. They key is not to overuse them.


So where would you say the use of singletons would be justified?

(And as Promit said, an awful lot of people will praise anything for its wonderful OO design as long as it has classes and singletons)

Share this post


Link to post
Share on other sites
Using a singleton can in many cases relieve you from the pain of passing references to commonly used components. In general, make your life easier. The negative side is that you make your components dependant on a global object, which might make the code less encapsulated and harder to reuse.

I used to be a big fan of the singleton pattern, but no so much anymore. I think it's a good pattern to use if your quite new and haven't really go to grips with OOP and software engineering yet.

Share this post


Link to post
Share on other sites
I agree with DarkZoulz. Using a singleton in the right place can make things a lot easier for you. I my case it makes code re-use a lot easier too.

If you want to be 100% "pure" OO and feel sigletons are not for you, don't use them. If you wish to get a job done and a singleton is the right tool... by all means, use it! "Pure OO" won't gain you anything if it makes your code less readable or harder to use.
That being said, don't use singletons just to use them. I know a few coders who use a certain design pattern just to brag how they're using this and that design pattern, even if it makes maintenance a nightmare for anyone else.


As an example: I have a singleton class called "Config". It can be used to read command-line parameters, environment variables and config files. The configuration is then available anywhere without the need to pass a reference to the class throughout the whole application. In my eyes a good thing.

It also serves it's purpose in remoting... the service agent is as simple as this:

public class ServiceRequestAgent : MarshalByRefObject
{
public void Set(string identifier, string value)
{
Config.Instance.Insert(identifier, value);
}

public string Get(string identifier)
{
return Config.Instance.Fetch(identifier);
}
}



When adding a new config parameter to the server I don't even have to touch the client or remoting part.

Share this post


Link to post
Share on other sites
1- The reason for the crash is probably part of T's code (which you didn't provide). As presented here, CSingletonT<int> (or any other basic type) would work flawlessly.

2- Why go to the pain of making your class a singleton when you can simply use a global instance ? Is the one-instance property really an essential requirement of the class (at which point, it should be made a singleton), or is it a consequence of how external code uses the class (at which point it is the responsibility of the using code to create one instance only) ? For example, the singleton in the above post should still work if there are several instances present, so I would strip it of its singleton status (if global access is for some reason necessary, just create a global instance).

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
So where would you say the use of singletons would be justified?

(And as Promit said, an awful lot of people will praise anything for its wonderful OO design as long as it has classes and singletons)


I have no wish to start yet another flamewar about singletons by arguing about their pros and cons, which are well documented. The original poster asked how to do a singleton template, and I answered :) . I'll leave it at that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!