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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:22:08+00:00 2026-06-01T07:22:08+00:00

So, classic simple Singleton realization is following: class Singleton { private: static Singleton* singleton;

  • 0

So, classic simple Singleton realization is following:

class Singleton
{
private:
    static Singleton* singleton;
    Singleton() {}
public:
    static Singleton* getInstance();        
};

cpp-file:

Singleton* Singleton::singleton = 0;

Singleton* Singleton::getInstance()
{
    if (!singleton)
    {
        singleton = new Singleton;
    }

    return singleton;
}

I see memory leak here – ‘cos there is no delete for the new. But in C++ there isn’t static destructor, so we just don’t care about this memory leak?

  • 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-01T07:22:09+00:00Added an answer on June 1, 2026 at 7:22 am

    A memory leak is more than just an allocation with no matching free. It’s when you have memory that could be reclaimed because the object is no longer in use, but which doesn’t ever actually get freed. In fact, many memory leaks are cases where there is code in the program to deallocate memory, but for whatever reason it doesn’t get called (for example, a reference cycle). There’s a lot of research on how to detect these sorts of leaks; this paper is an excellent example of one such tool.

    In the case of a singleton, we don’t have a leak because that singleton exists throughout the program. Its lifetime is never intended to end, and so the memory not getting reclaimed isn’t a problem.

    That said, the code you have above is not how most people would implement a singleton. The canonical C++ implementation would be something like this:

    class Singleton
    {
    private:
        /* No instantiation. */
        Singleton() {}
    
        /* Explicitly disallow copying. */ 
        Singleton(const Singleton&) = delete;
        Singleton& operator= (const Singleton&) = delete;
    
        /* In C++03, the above would be written as
         *
         *    Singleton(const Singleton&);
         *    Singleton& operator= (const Singleton&);
         * 
         * and you'd just leave the methods unimplemented.
         */
    public:
        static Singleton& getInstance();        
    };
    

    .cpp file:

    Singleton& Singleton::getInstance() {
        /* Have a static local variable representing the unique instance.  Since
         * it's static, there is only one instance of this variable.  It's also only
         * initialized when getInstance is called.
         */
        static Singleton theInstance;
        return theInstance;
    }
    

    Now there’s no dynamic allocation at all – the memory is allocated by the compiler and probably resides in the code or data segment rather than in the heap. Also note that you have to explicitly disallow copying, or otherwise you could end up with many clones of the singleton.

    The other advantage of this is that C++ guarantees that at program exit (assuming the program terminates normally), the destructor for theInstance will indeed fire at the end of the program. You can thus define a destructor with all the cleanup code you need.

    Hope this helps!

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

Sidebar

Related Questions

Classic example of a simple server: class ThreadPerTaskSocketServer { public static void main(String[] args)
The classic of writing a singleton in java is like this: public class SingletonObject
One my home works in my classic ASP class is to create a simple
What I am trying to do is simple. I have some Classic ASP with
In classic asp you had the Global.asa file, what's the equivalent in .Net? I
Are there any simple encrypt/decrypt functions in Classic ASP? The data that needs to
We're implementing a new solution in our classic ASP environment that's using COM interop
I have a very simple and classic installation: Windows 7 WampServer with PEAR (PHP
I'm converting a classic ASP page to ASP.NET MVC 2. I have a simple
This should be the classic simple error which I can't really find.. I am

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.