Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:05:04+00:00 2026-05-23T01:05:04+00:00

The idea is to have a Singleton in C++ deleted when the program ends.

  • 0

The idea is to have a Singleton in C++ deleted when the program ends. We learned this method of implementation in class:

class Singleton
{

private:
    static Singleton* the_singleton;

protected:
    Singleton()
    {
        static Keeper keeper(this);
        /*CONSTRUCTION CODE*/
    }
    virtual ~Singleton()
    {
        /*DESTRUCTION CODE*/
    }

public:
    class Keeper
    {

    private:
        Singleton* m_logger;

    public:
        Keeper(Singleton* logger):m_logger(logger){}

        ~Keeper()
        {
            delete m_logger;
        }
    };
    friend class Singleton::Keeper;

    static Singleton* GetInstance();
    {
        if (!the_singleton)
            the_singleton = new Singleton();
        return the_singleton;
    }
};

Singleton* Singleton::the_singleton = NULL;

The idea is that on the first time the Singleton is created, a static Keeper object will be created in the Singleton’s C’tor, and once the program ends, that Keeper will be destroyed, and in turn will destroy the Singleton’s instance it is pointing to.

Now, this method seems quite cumbersome to me, so I suggested to ditch the keeper class and make the Singleton’s instance a static object of the getInstance method:

<!-- language: c++ -->

class Singleton
{

protected:
    Singleton()
    {
        /*CONSTRUCTION CODE*/
    }

    ~Singleton()
    {
        /*DESTRUCTION CODE*/
    }

public:
    static Singleton &getInstance()
    {
        static Singleton instance;
        return instance;
    }

    /*OBJECT FUNCTIONALITY*/
};

That way, the Singleton is constructed on the first call to the getInstance method, and destroyed once the program ends. No need for that keeper class.

I tested it and it worked just fine – the Singleton was created and destroyed at the right places. However, my TA said that this pattern is wrong, though he couldn’t recall what exactly was the problem with it. So I’m hoping someone here has encountered this implementation before and can tell me what’s wrong with it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:05:05+00:00Added an answer on May 23, 2026 at 1:05 am

    There’s no problem with it at all. The “Keeper” thing is nuts for this application, IMHO.

    There are a few cases where it might be warranted. For instance, if the Singleton must take constructor arguments, allocating it statically might not be possible. Or if its constructor might fail, the simpler approach wouldn’t allow you to recover or retry. So in some circumstances, doing something more complex might be needed. In many cases, though, it’s unnecessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some class it is a singleton we have this class in already
Okay, this is just a crazy idea I have. Stack Overflow looks very structured
I have this idea for a free backup application. The largest problem I need
I have an idea for how to solve this problem, but I wanted to
I have no idea. This causes seemingly random time-outs. These in turn break the
I have a singleton timer in my WP7 application however I have no idea
I have a singleton ejb which is getting initialised twice. I have no idea
Scenario: I have @Singleton UserFactory ( @Stateless could be) , its method createSession() generating
I have a Singleton class that manages the connection to an external device. The
I have a singleton class based on Joose and I would like to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.