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

  • Home
  • SEARCH
  • 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 504465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:29:59+00:00 2026-05-13T06:29:59+00:00

I have a singleton class, whose instance get initialized at global scope within the

  • 0

I have a singleton class, whose instance get initialized at global scope within the class’s CPP file:

Singleton* Singleton::uniqueInstance = new Singleton();

Its header file looks like:

class Singleton {
public:
    static Singleton& getInstance() { return *uniqueInstance; }
    static bool destroyInstance() { delete uniqueInstance; }

private:
    //...
    //... typical singleton stuff
    static Singleton* uniqueInstance;
}; // end of class Singleton

I noticed that its destructor dodn’t get executed during program termination, thus I added a public static interface Singleton::destroyInstance(), to be manually invoke by client code before the program exits, for instance deletion. This snippet is not the complete code, and assumed that there are other codes that dealing with thread safety issue. In this case, how can I make use of RAII to eliminate the need of introducing such an interface? Thanks for advice.

  • 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-13T06:29:59+00:00Added an answer on May 13, 2026 at 6:29 am

    Singleton are easy to create, unless you are in a MultiThreaded environment. If you can, you’ll want to make sure that only one thread is trying to create your singleton (and thus destroy it). Once created it may be used by multiple threads at once… though that may not be the best way to deal with this.

    Anyway, a very simplistic option is thus:

    class MySingleton
    {
    public:
      static MySingleton& Intance() { static MySingleton M_Instance; return M_Instance; }
    private:
      MySingleton() {}
    };
    

    It works well as long as you’re not using MI and you do not use the singleton during globals destructions. Also it does not work with VC2003 at least (used to instantiate one singleton for each library the method was called….), I don’t know with more recent versions, we’ve stopped compiling on windows altogether a while ago.

    Now, if you want to really learn more about Singleton:

    • instanciation and destruction issues
    • lifetime variety
    • threading safety

    Alexandrescu concentrated on this for a full chapter in Modern C++ Design, if you don’t have access to the book you can still read the code that resulted from the reflexion in Loki.

    Also, you might simply use alternative approaches to the design. Singleton may make testing difficult, there have been interesting thoughts about this from the people that support Dependency Injection. Take a peek at Misko Hevery blog entry.

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

Sidebar

Ask A Question

Stats

  • Questions 300k
  • Answers 300k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ECMAScript 5 will have seal() and freeze(), but there's no… May 13, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer No. A C cast can do the equivalent of a… May 13, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer (Using Reflector) Path.GetTempPath() ultimately calls the Win32 function GetTempPath (from… May 13, 2026 at 7:59 pm

Related Questions

I was thinking about the classic issue of lazy singleton initialization - the whole
Ok, this is veeery weird... I think. What I mean with the title is:
One of the libraries we are using for our product uses a singleton for
I have an object cache which implements the Singleton design pattern. My approach to
I'm trying to fix a design flaw that I recently ran across in some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.