Singelton life time inside dll / bundle

If I create a singleton class in the context of a dll or bundle on mac, the singleton class is created once and is used by all dll instances. I am using dll as a plugin for an application. Now the following thought occurred to me: if I use the Singleton Class, it will be used for several instances of the plug-in. However, this makes it difficult to effectively manage the lifetime of a singleton class. The only way I could think of is to use the reference count and have singleton delete it myself when the reference count is 0.

Does anyone have any better ideas? Is there a good way to limit a singleton object to a single dll instance?

The language is C ++, but the solution should work under windows and mac (being a bunch here). The specification for dll or bundle is given by the application, so nothing will change here.

+3
source share
2 answers

Here is my golden rule regarding DLL and C ++.

The internal DLL code can be written in C ++, but only C functions should be exported from the DLL. But you can have "factory" functions that return C ++ interface pointers

It just gets confused trying to export each method from each class from a DLL. As for components and interfaces.

DLL, Windows. COM - . COM - . COM-DLL, . ATL (CComObjectRootEx ), . ++ COM. , COM- "" singleton.

, , . ( COM-, - DLL, , IDispatch - ).

, COM, , DLL COM.

// singleton.h (consumed by DLL and users of your singleton)
// IMySingleton is an interface class.  It only defines pure virtual methods
class IMySingleton
{
public:
    virtual int DoSomething()=0;
    virtual int DoSomethingElse()=0;

    virtual void AddRef()=0;
    virtual void Release()=0;
};

// returns back an instance of IMySingleton with the refcount already at "1"
// This function gets exported out of your DLL(either via .def file or __declexport attribute)
HRESULT GetMySingletonInstance(IMySingleton** ppMySingleton);
// -------------------------------------



// singleton.cpp (compiled into your DLL - but is NOT exported out of the DLL)
class CMySingleton : public IMySingleton
{
public:
    int m_refcount;
    static CMySingleton* s_pInstance;

    // returns an adref'd instance of the singleton
    static CMySingleton* GetInstance()
    {
        if (s_pInstance == NULL)
        {
           s_pInstance = new CMySingleton(); // refcount at 1
        }
        else
        {
           s_pInstance->AddRef();
        }

        return s_pInstance;
    }

    CMySingleton()
    {
       m_refcount = 1;
       // your initialization code goes here
    }

    ~CMySingleton()
    {
       // cleanup code
    }

    int DoSomething()
    {
        /// your code goes here
        return x;
    }

    int DoSomethingElse()
    {
        /// your code goes here
        return y;
    }
    void AddRef() {m_refcount++;}
    void Release()
    {
        m_refcount--;
        if (m_refcount == 0)
        {
            s_pInstance = NULL;
            delete this;
        }
    }

};

// exported out of the DLL
HRESULT GetMySingletonInstance(IMySingleton** ppSingleton)
{
    *ppSingleton = static_cast<IMySingleton*>(CMySingleton::GetInstance());
}

, singleton, GetMySingletonInstance. , singleton, Release() , .

+1
0

Source: https://habr.com/ru/post/1793468/


All Articles