diff plugin/FeatureExtractionPluginFactory.cpp @ 298:57d7889f626c

* Unload feature extraction plugin .so when destroyed
author Chris Cannam
date Fri, 28 Sep 2007 14:32:45 +0000
parents 524bcd89743b
children 576be0d0d218
line wrap: on
line diff
--- a/plugin/FeatureExtractionPluginFactory.cpp	Fri Sep 28 13:56:38 2007 +0000
+++ b/plugin/FeatureExtractionPluginFactory.cpp	Fri Sep 28 14:32:45 2007 +0000
@@ -18,6 +18,7 @@
 
 #include "vamp/vamp.h"
 #include "vamp-sdk/PluginHostAdapter.h"
+#include "vamp-sdk/hostext/PluginWrapper.h"
 
 #include "system/System.h"
 
@@ -30,6 +31,25 @@
 
 //#define DEBUG_PLUGIN_SCAN_AND_INSTANTIATE 1
 
+class PluginDeletionNotifyAdapter : public Vamp::HostExt::PluginWrapper {
+public:
+    PluginDeletionNotifyAdapter(Vamp::Plugin *plugin,
+                                FeatureExtractionPluginFactory *factory) :
+        PluginWrapper(plugin), m_factory(factory) { }
+    virtual ~PluginDeletionNotifyAdapter();
+protected:
+    FeatureExtractionPluginFactory *m_factory;
+};
+
+PluginDeletionNotifyAdapter::~PluginDeletionNotifyAdapter()
+{
+    // see notes in vamp-sdk/hostext/PluginLoader.cpp from which this is drawn
+    Vamp::Plugin *p = m_plugin;
+    delete m_plugin;
+    m_plugin = 0;
+    if (m_factory) m_factory->pluginDeleted(p);
+}
+
 static FeatureExtractionPluginFactory *_nativeInstance = 0;
 
 FeatureExtractionPluginFactory *
@@ -276,6 +296,7 @@
 						  float inputSampleRate)
 {
     Vamp::Plugin *rv = 0;
+    Vamp::PluginHostAdapter *plugin = 0;
 
     const VampPluginDescriptor *descriptor = 0;
     int index = 0;
@@ -328,7 +349,12 @@
         goto done;
     }
 
-    rv = new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
+    plugin = new Vamp::PluginHostAdapter(descriptor, inputSampleRate);
+
+    if (plugin) {
+        m_handleMap[plugin] = libraryHandle;
+        rv = new PluginDeletionNotifyAdapter(plugin, this);
+    }
 
 //    std::cerr << "FeatureExtractionPluginFactory::instantiatePlugin: Constructed Vamp plugin, rv is " << rv << std::endl;
 
@@ -346,6 +372,17 @@
     return rv;
 }
 
+void
+FeatureExtractionPluginFactory::pluginDeleted(Vamp::Plugin *plugin)
+{
+    void *handle = m_handleMap[plugin];
+    if (handle) {
+        std::cerr << "unloading library " << handle << " for plugin " << plugin << std::endl;
+        DLCLOSE(handle);
+    }
+    m_handleMap.erase(plugin);
+}
+
 QString
 FeatureExtractionPluginFactory::getPluginCategory(QString identifier)
 {