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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:25:01+00:00 2026-05-25T12:25:01+00:00

I have a singleton (I know that is a bad pattern). To control the

  • 0

I have a singleton (I know that is a bad pattern). To control the cleaning process, I’m using a shared pointer. The relevant code is:

#ifndef _GLOBAL_LOG_H_
#define _GLOBAL_LOG_H_

    namespace glog{

        class CGlobalLog;
        typedef boost::shared_ptr<CGlobalLog> globalLogPtr;

        class CGlobalLog
        {
        private:

            static globalLogPtr m_instance;
            LogLevel minimiumLogLevel;
            CGlobalLog(void);

            static void deleter(CGlobalLog *ptr){
                try{
                    delete ptr;
                }
                catch(std:: e)
                {
                    std::cout << e.what() << "\n";
                }
            }
            static void create() { m_instance.reset( new CGlobalLog, &CGlobalLog::deleter );   }  
            void addMessage_(const std::string& appender, LogLevel level /*= LOGLEVEL_INFO*/,const char* msg, va_list args );
            ~CGlobalLog(void);
        public:         
            static globalLogPtr& getInstance();
            void addMessage(const std::string& message, std::string appender, LogLevel level = LOGLEVEL_INFO);

        };
        globalLogPtr CGlobalLog::m_instance;
    };

#endif // _GLOBAL_LOG_H_

The program works fine, but when program finish, an unhandled exception is thrown in this point:

static void deleter(CGlobalLog *ptr){
    try{
        delete ptr; //<-- Unhandled exception
    }
    catch(std:: e)
    {
        std::cout << e.what() << "\n";
    }
}

The catch doesn’t catch the exception so I don’t know what to do to profile my error. The exact code where error is throw is a boost library file checked_delete.hpp, here:

// verify that types are complete for increased safety

template<class T> inline void checked_delete(T * x)
{
    // intentionally complex - simplification causes regressions
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete x;
}

How do I need to locate this error? Some ideas?

Thanks!!!

  • 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-25T12:25:01+00:00Added an answer on May 25, 2026 at 12:25 pm

    I generally don’t expect to see a shared pointer on a singleton. Just returning a reference to your singleton and never keeping a reference to it laying around is a good practice.

    struct Foo {
      static Foo &instance() {
        static Foo foo;
        return foo;
      }
    };
    
    struct Bar {
      void someMethod() {
        Foo &foo = Foo::instance(); // just grab a reference every time you need it
        // ...
      }
    };
    

    If you wish to keep the shared pointer and need to clean up resources in manual way, create a tear down method. The boost::shared_ptr will clean up the memory eventually.

    Personally, I think using a shared pointer externally is inferior. I wrote some code to demonstrate a tear down and it didn’t seem generally applicable without knowing why you need one.

    If you want an explicit delete, then write one.

    struct Foo {
      static Foo *foo = 0;
      static Foo &instance() {
        if (!foo)
          throw std::logic_error("Already deleted");
        return *foo;
      }
      static void Init() {
        if (foo)
          throw std::logic_error("Already created");
        foo = new Foo;
      }
      static void Destroy() {
        if (!foo)
          throw std::logic_error("Already deleted");
        delete foo;
        foo = 0;
      }
    };
    

    In the case of logging, the logic errors should be superfluous. If logging isn’t valid when you ask for it, then it is unlikely that your application is in a valid state.

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

Sidebar

Related Questions

I know that Singleton pattern is bad because it uses global state. But in
If I'm using a singleton pattern (yeah, I know - they're usually bad)... and
I have a very bad feeling about using lock in my code but now
I've got code that I am using from another team and I have spent
I have read multiple articles about why singletons are bad. I know it has
I have a singleton that uses the static readonly T Instance = new T();
I have a Singleton that is accessed in my class via a static property
I have a singleton object that use another object (not singleton), to require some
I have a C# singleton class that multiple classes use. Is access through Instance
I have a DAL that is replicated across multiple apps (I know its a

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.