Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's my golden rule with respect to DLLs and C++.</p> <p><em><strong>The code internally to the DLL can be written in C++, but only C functions should get exported from the DLL. But you can have "factory" functions that return back c++ interface pointers</em></strong></p> <p>It just gets messy trying to export every method of every class out of a DLL. Thing in terms of components and interfaces.</p> <p>Since you said DLL, this implies Windows. <a href="http://en.wikipedia.org/wiki/Component_Object_Model" rel="nofollow">COM</a> is the answer. COM is your friend. Make a COM DLL and your reference counting problem is mostly solved. Use ATL template libraries (CComObjectRootEx and friends) to make implementation really easy. It gets a little tricky to export a C++ class singleton with COM. Easiest approach is to have a COM class "wrap" the singleton and just forwards all method calls to the real singleton instance.</p> <p>Now if you are not familiar with the component object model, it can be a little steep in the learning curve. (And most of the reference books spend way too much time on out-of-process COM, proxy/stub DLLs, automation, IDispatch - none of which is relevant to your problem).</p> <p>Now if you don't have time to learn COM, here's a rough framework on how to implement the DLL singleton pattern without COM.</p> <pre><code>// 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-&gt;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&lt;IMySingleton*&gt;(CMySingleton::GetInstance()); } </code></pre> <p>Callers that need to access the singleton's services need only call GetMySingletonInstance once. When they go away, they just release the singleton instance by calling Release() on the pointer instance they have.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload