在共享库中调用静态对象的析构函数

时间:2020-07-22 08:23:11

标签: c++ windows dll

我已经实现了Meyers的单例和四个功能来使用它。例如:

//A.h
class A
{
private:
  Device d;
public:
  A(){//...}
  ~A(){d.release();}
}

我班的单身汉Meyers

//Controller.h
class Controlle
{
private:
//some members
 A m_member;

 Controller();
 Controller(const Controller&);
 Controller& operator=(const Controller&);
public:
  static Controller& GetInstance()
  {
    static Controller instance;
    return instance;
  }
}

以及用于处理Controller类的接口

extern "C" API Result InitDevice(const ParamsDevice* param);
extern "C" API Result InitAlgorithm(const ParamAlgorithm* param);
extern "C" API Result Operation_1();
extern "C" API Result Operation_2();

我构建该库并获取Controller.dll和Controller.lib文件。库是共享库。 然后,我创建一个使用Controller.dll的程序(隐式)。程序终止后,将引发运行时库异常。我在调试中编译了该库,以检查从何处抛出异常。 我从方法Release()类的comip.h文件中获取异常

template<typename _IIID> class _com_ptr_t
{
public:
    // Declare interface type so that the type may be available outside
    // the scope of this template.
    //
    typedef _IIID ThisIIID;
    typedef typename _IIID::Interface Interface;
    ........
    // Provides error-checking Release()ing of this interface.
    //
    void Release()
    {
        if (m_pInterface == NULL) {
           _com_issue_error(E_POINTER);
        }
        else {
           m_pInterface->Release();
           m_pInterface = NULL;
        }
    }
}

我认为程序终止时,库中使用的静态对象的资源被错误地释放。如果我用经典的singelton代替singelton Meyers并创建用于显式释放静态指针内存的方法,那么一切都会正常进行。您能帮我解释一下这种影响吗?

我认为我的问题有一个答案,但这对我来说还不完整,我无法理解 https://stackoverflow.com/a/50644725/13970084

P.S。对不起我的英语不好。英语不是我的母语。

0 个答案:

没有答案
相关问题