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 8572663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:04:24+00:00 2026-06-11T19:04:24+00:00

I know 2 methods to make singleton pattern like this: class sgt_static { sgt_static()

  • 0

I know 2 methods to make singleton pattern like this:

class sgt_static
{
    sgt_static() { }
    public:
        static sgt_static* get_instance()
        {
            static sgt_static instance;
            return &instance;
        }
}

and this:

class sgt_new
{
    sgt_new() { }
    public:
         static sgt_new* get_instance()
         {
             static sgt_new* instance = NULL;
             if ( instance == NULL ) instance = new sgt_new();
             return instance;
         }
}

I knew some differences between them:

  1. Instance of sgt_new should be delete by myself.
  2. If the program exits(normally), instance of sgt_static would be delete by program itself(or by OS?).

But I set breakpoints in ~sgt_new(), when my program exits, debugger has no any breaking action at all. Someone says that OS would reclaims these resources back. Is that true ? So, instance of sgt_new won’t lead to any memory leak?

Aparts from points above of my list. Are other differences between 2 implementation of singleton?

  • 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-06-11T19:04:25+00:00Added an answer on June 11, 2026 at 7:04 pm

    Objects on the heap are not deleted by the C++ system unless something calls delete. That is, the object in your second version won’t get destroyed. Depending on how the object is used this may be quite intentional: While wrapping the object with a function guarantees that the object won’t be acced before it is constructed there is mothing which can be done that it is accessed after it has been destroyed. After all, there is no control to where the pointer got passed.

    When a program terminates, for whatever readon, the resources are reclaimed from outside the program. When and how this is done depends on the operating system and differnt resources may be reclaimed at different times. Note that this resource clean-up does not call destructors but merely makes resources like file descriptors (or equivalent), memory, locks, etc. available. That is, if your singleton’s destructor has any important object you probably want to use the first approach.

    That said, singletons are massively over-used as some sort of suspected to be glorified global memory! The are a few legitimate uses of singletons but probability indicates
    your case is not one of them: less than 1% of singleton uses are legitemate based on my running count. The way to determine if a singleton is to determine if it could be conceivable if two (or more) versions of the program running in the same executable still can use the same singleton. If not, it is not a legitemate use of a singleton.

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

Sidebar

Related Questions

I know this represents the object invoking the method and static methods are not
Let's say I have a singleton class like this: class Settings include Singleton def
I know the naming convention for class methods in C# is to begin with
As you probably know, all class methods of CCFileUtils in Cocos2D-iphone 1.x were rewritten
I know two methods of copying a postgres database, but both of them require
I know of two methods which are not reliable: _access() - doesn't work on
I know you can restrict which HTTP methods a particular ActionResult method responds to
I know that classes can implement various special methods, such as __iter__ , __setitem__
I know you can override delete and save methods in DJango models, but can
I know there are at least three popular methods to call the same function

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.