4

I have implemented singleton of Meyers and four function to work with it. For example:

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

My class singleton 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;
  }
}

And interface for working the class of 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();

I build the library and get the Controller.dll and Controller.lib files. Library is shared lib. Then I create a program where Controller.dll is used (implicit). Runtime library exception is thrown after program termination. I compiled the library in debug to check where the exception is thrown from. I am getting the exception from comip.h file from method Release() class

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;
        }
    }
}

I think that when the program terminates, the resources of static objects that are used in the library are incorrectly released. If I replace the singelton Meyers with a classic singelton and create methods for explicitly freeing up the memory of a static pointer, then everything works well. Could you help me explain this effect?

I think there is an answer to my question, but it is not complete for me and I cannot understand https://stackoverflow.com/a/50644725/13970084

P.S. Sorry for my English. English isn't my native language.

Lucifer
  • 53
  • 4
  • This is a library OpenCV 4.2. Device is VideoCapture. If I run a program that will directly use the code that is implemented in the library, then everything ends well, there are no errors. I can call the method 'release()' explicit but I don't have to call it, it will call itself in the destructor. It seems to me that for some reason the library is unloaded first than the destructor is called. – Lucifer Jul 22 '20 at 09:46
  • I seem to have found a similar problem: https://comp.os.ms-windows.programmer.win32.narkive.com/YQ17VVny/static-object-destructor-and-dll-unloading – Lucifer Jul 22 '20 at 11:24

0 Answers0