comparison 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
comparison
equal deleted inserted replaced
97:ca44b3594d2e 98:896a97349ac5
84 84
85 PluginCategoryHierarchy getPluginCategory(PluginKey key); 85 PluginCategoryHierarchy getPluginCategory(PluginKey key);
86 86
87 string getLibraryPathForPlugin(PluginKey key); 87 string getLibraryPathForPlugin(PluginKey key);
88 88
89 static void setInstanceToClean(PluginLoader *instance);
90
89 protected: 91 protected:
90 class PluginDeletionNotifyAdapter : public PluginWrapper { 92 class PluginDeletionNotifyAdapter : public PluginWrapper {
91 public: 93 public:
92 PluginDeletionNotifyAdapter(Plugin *plugin, Impl *loader); 94 PluginDeletionNotifyAdapter(Plugin *plugin, Impl *loader);
93 virtual ~PluginDeletionNotifyAdapter(); 95 virtual ~PluginDeletionNotifyAdapter();
94 protected: 96 protected:
95 Impl *m_loader; 97 Impl *m_loader;
96 }; 98 };
97 99
100 class InstanceCleaner {
101 public:
102 InstanceCleaner() : m_instance(0) { }
103 ~InstanceCleaner() { delete m_instance; }
104 void setInstance(PluginLoader *instance) { m_instance = instance; }
105 protected:
106 PluginLoader *m_instance;
107 };
108
98 virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter); 109 virtual void pluginDeleted(PluginDeletionNotifyAdapter *adapter);
99 110
100 map<PluginKey, string> m_pluginLibraryNameMap; 111 map<PluginKey, string> m_pluginLibraryNameMap;
101 bool m_allPluginsEnumerated; 112 bool m_allPluginsEnumerated;
102 void enumeratePlugins(PluginKey forPlugin = ""); 113 void enumeratePlugins(PluginKey forPlugin = "");
113 void unloadLibrary(void *handle); 124 void unloadLibrary(void *handle);
114 void *lookupInLibrary(void *handle, const char *symbol); 125 void *lookupInLibrary(void *handle, const char *symbol);
115 126
116 string splicePath(string a, string b); 127 string splicePath(string a, string b);
117 vector<string> listFiles(string dir, string ext); 128 vector<string> listFiles(string dir, string ext);
129
130 static InstanceCleaner m_cleaner;
118 }; 131 };
119 132
120 PluginLoader * 133 PluginLoader *
121 PluginLoader::m_instance = 0; 134 PluginLoader::m_instance = 0;
122 135
136 PluginLoader::Impl::InstanceCleaner
137 PluginLoader::Impl::m_cleaner;
138
123 PluginLoader::PluginLoader() 139 PluginLoader::PluginLoader()
124 { 140 {
125 m_impl = new Impl(); 141 m_impl = new Impl();
126 } 142 }
127 143
131 } 147 }
132 148
133 PluginLoader * 149 PluginLoader *
134 PluginLoader::getInstance() 150 PluginLoader::getInstance()
135 { 151 {
136 if (!m_instance) m_instance = new PluginLoader(); 152 if (!m_instance) {
153 // The cleaner doesn't own the instance, because we leave the
154 // instance pointer in the base class for binary backwards
155 // compatibility reasons and to avoid waste
156 m_instance = new PluginLoader();
157 Impl::setInstanceToClean(m_instance);
158 }
137 return m_instance; 159 return m_instance;
138 } 160 }
139 161
140 vector<PluginLoader::PluginKey> 162 vector<PluginLoader::PluginKey>
141 PluginLoader::listPlugins() 163 PluginLoader::listPlugins()
174 { 196 {
175 } 197 }
176 198
177 PluginLoader::Impl::~Impl() 199 PluginLoader::Impl::~Impl()
178 { 200 {
201 }
202
203 void
204 PluginLoader::Impl::setInstanceToClean(PluginLoader *instance)
205 {
206 m_cleaner.setInstance(instance);
179 } 207 }
180 208
181 vector<PluginLoader::PluginKey> 209 vector<PluginLoader::PluginKey>
182 PluginLoader::Impl::listPlugins() 210 PluginLoader::Impl::listPlugins()
183 { 211 {