diff vamp-sdk/hostext/PluginLoader.cpp @ 98:896a97349ac5

* Add a static wrapper object to wrap the static instance pointer, so that it can be properly deleted on program exit instead of showing up in certain tools as leaked
author cannam
date Mon, 03 Dec 2007 12:57:27 +0000
parents ca44b3594d2e
children 44e6b94c2696
line wrap: on
line diff
--- a/vamp-sdk/hostext/PluginLoader.cpp	Sun Nov 18 10:53:54 2007 +0000
+++ b/vamp-sdk/hostext/PluginLoader.cpp	Mon Dec 03 12:57:27 2007 +0000
@@ -86,6 +86,8 @@
 
     string getLibraryPathForPlugin(PluginKey key);
 
+    static void setInstanceToClean(PluginLoader *instance);
+
 protected:
     class PluginDeletionNotifyAdapter : public PluginWrapper {
     public:
@@ -95,6 +97,15 @@
         Impl *m_loader;
     };
 
+    class InstanceCleaner {
+    public:
+        InstanceCleaner() : m_instance(0) { }
+        ~InstanceCleaner() { delete m_instance; }
+        void setInstance(PluginLoader *instance) { m_instance = instance; }
+    protected:
+        PluginLoader *m_instance;
+    };
+
     virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter);
 
     map<PluginKey, string> m_pluginLibraryNameMap;
@@ -115,11 +126,16 @@
 
     string splicePath(string a, string b);
     vector<string> listFiles(string dir, string ext);
+    
+    static InstanceCleaner m_cleaner;
 };
 
 PluginLoader *
 PluginLoader::m_instance = 0;
 
+PluginLoader::Impl::InstanceCleaner
+PluginLoader::Impl::m_cleaner;
+
 PluginLoader::PluginLoader()
 {
     m_impl = new Impl();
@@ -133,7 +149,13 @@
 PluginLoader *
 PluginLoader::getInstance()
 {
-    if (!m_instance) m_instance = new PluginLoader();
+    if (!m_instance) {
+        // The cleaner doesn't own the instance, because we leave the
+        // instance pointer in the base class for binary backwards
+        // compatibility reasons and to avoid waste
+        m_instance = new PluginLoader();
+        Impl::setInstanceToClean(m_instance);
+    }
     return m_instance;
 }
 
@@ -178,6 +200,12 @@
 {
 }
 
+void
+PluginLoader::Impl::setInstanceToClean(PluginLoader *instance)
+{
+    m_cleaner.setInstance(instance);
+}
+
 vector<PluginLoader::PluginKey>
 PluginLoader::Impl::listPlugins() 
 {