If I create a singleton class in the context of a dll or bundle on mac, the singleton class is instantiated once and used by all instances of the dll. I am using a dll as a plug-in for an application. Now the following thing came to my mind: If I use a singleton Class, it will be shared across multiple instances of the plug-in. However, this makes it difficult to manage the lifetime of the singleton class efficiently. The only way I could think of would be to use a reference count and to make the singleton delete its self when the reference count is 0.
Does anyone have any better ideas on that? Is there any good way to restrict the singleton object to one instance of the dll?
Language is c++, but the solution has to work under windows and mac (being a bundle here). The specification for the dll or bundle are given by the application, so there is nothing to change here.
Typically a static object will not be shared across multiple instances of an application or library. Each application has it's own address space and can not access other applications memory. Are you perhaps using shared memory or inter-process communication?
Using a reference count is an excellent idea, there are several so-called
"shared-pointer"
libraries available that can help you with implementing it. Here is an implementation I often use: http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/shared_ptr.htm