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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:28:01+00:00 2026-05-27T19:28:01+00:00

I was implementing a singleton pattern.Here,I am creating a new instance of Singleton* in

  • 0

I was implementing a singleton pattern.Here,I am creating a new instance of Singleton* in GetInstance, when I try and delete it in the destructor, it does in infinite loop. How to avoid memory leak in this case ?

Please refer the below piece of code:

#define NULL 0
class Singleton  
{ 
    private :  
        static Singleton* m_pInstance;  
        Singleton(){};  

    public :

    static Singleton* GetInstance()
    {
        if(m_pInstance == NULL)
        {
            m_pInstance  = new Singleton();         
        }
        return m_pInstance;
    }

    ~Singleton()
    { 
        //delete m_pInstance; // The system goes in infinate loop here if i uncomment this  
        m_pInstance = NULL;
    }
};

Singleton*  Singleton ::m_pInstance = NULL;   

int main()  
{
    Singleton* pInstance = Singleton::GetInstance();
    delete pInstance;  
}     
  • 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-27T19:28:02+00:00Added an answer on May 27, 2026 at 7:28 pm

    Of course it causes an infinite loop !

    You call the destructor, but the destructor also calls the destructor, so the destructor calls the destructor again… and again…

    If you want to use delete, you must use it from outside of the destructor and NOT call it again in the destructor.

    To do that, you can use another static method which will mirror the GetInstance() method :

    class Singleton  
    { 
    public :
    
       ...
    
       // this method is a mirror of GetInstance
       static void ResetInstance()
       {
          delete m_pInstance; // REM : it works even if the pointer is NULL (does nothing then)
          m_pInstance = NULL; // so GetInstance will still work.
       }
    
       ...
    
       ~Singleton()
       { 
           // do destructor stuff : free allocated resources if any.
           ...
       }
    

    Note : the other people warn you about using a singleton and they are right because this pattern is often misused. So think before using it. But go ahead anyway, that is the good way to learn !

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

Sidebar

Related Questions

I have a question about the Singleton Pattern and MEF. I'm new in implementing
In android, are using static variables a recommended practice? E.g, implementing a Singleton pattern
Due to the flooding examples of implementing logger using Singleton pattern, I have just
When implementing a singleton in C++, is it better for GetInstance() to return a
I come up with this question when implementing singleton pattern in Java. Even though
I've got an application where I'm implementing a singleton pattern in the database, and
I'm using singleton design-pattern for my database connection. Here is my method. private static
I've typically gotten used to implementing a singleton pattern in this manner because it's
I did see on internet some pattern for implementing singleton class as following: class
Does anyone have experience (Django 1.x pref 1.3) with implementing a sort of singleton

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.