
Ben Young wrote:
On Tuesday 27 July 2004 07:21, Vladimir Prus wrote:
Do you mean that reference counting should be done by DLL itself? Why not:
template<class T> class plugin_ptr { public: // operator-> and friends private: T* m_ptr; shared_ptr<dll_handle> m_dll; }
So, 'dll' won't be deleted until all plugins which use it are dead.
The way we do that at my company is that all objects returned by a plugin are held my a shared_ptr which has a custom deleter which decrements a ref count on the library itself.
Something like void* h = dlopen(......) ; boost::shared_ptr<Plugin> p( ...... dlsym(.....), bind(dlclose, h)) ? That's interesting, but is raises two questions: - What if you get two plugins from the same dll? - If dll can be reloaded, then, as I mention in another email, we'd need to notify all 'plugin_map' instances when dll is reloaded, so dll_handle must be a bit more smart. What I'm thinking about now is: class dll_handle : boost::noncopyable { public: // Opens the specified library. If it's already opened, returns // pointer to an existing dll_handle. shared_ptr<dll_handle> get(const std::string& name); void* operator[](const std::string& symbol_name); private: dll_handle(const std::string& name); }; class dll { public: dll(const std::string& name); // default copy-ctor is OK template<.......> .... call(.......); template<......> .... import(.......); private: boost::shared_ptr<dll_handle> m_handle; }; class plugin_ptr { public: // Minimum set of smart_ptr methods private: // Just to keep the DLL in memory when plugin is in memory shared_ptr<dll_handle> m_handle; }; class plugin_map { public: ... interface to be yet defined ... }; - Volodya