简体   繁体   English

应用程序完成时未处理的异常

[英]Unhandled exception when application is finishing

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. catch不会捕获异常,因此我不知道该如何解决我的错误。 The exact code where error is throw is a boost library file checked_delete.hpp, here: 引发错误的确切代码是增强库文件checked_delete.hpp,这里:

// 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!!! 谢谢!!!

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. boost::shared_ptr最终将清理内存。

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. 如果您要求日志记录无效,则您的应用程序不太可能处于有效状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM