comparison src/vamp-sdk/PluginAdapter.cpp @ 467:bd8484e19353 vampipe

Fix long-standing memory leaks (parameter descriptors and feature list container) in plugin adapter
author Chris Cannam
date Fri, 14 Oct 2016 09:32:11 +0100
parents abdf03252c8a
children 59f7c7ae2f64
comparison
equal deleted inserted replaced
466:a94ab90dfd53 467:bd8484e19353
164 #ifdef DEBUG_PLUGIN_ADAPTER 164 #ifdef DEBUG_PLUGIN_ADAPTER
165 std::cerr << "PluginAdapterBase::Impl[" << this << "]::getDescriptor" << std::endl; 165 std::cerr << "PluginAdapterBase::Impl[" << this << "]::getDescriptor" << std::endl;
166 #endif 166 #endif
167 167
168 if (m_populated) return &m_descriptor; 168 if (m_populated) return &m_descriptor;
169 169
170 Plugin *plugin = m_base->createPlugin(48000); 170 Plugin *plugin = m_base->createPlugin(48000);
171 171
172 if (!plugin) { 172 if (!plugin) {
173 std::cerr << "PluginAdapterBase::Impl::getDescriptor: Failed to create plugin" << std::endl; 173 std::cerr << "PluginAdapterBase::Impl::getDescriptor: Failed to create plugin" << std::endl;
174 return 0; 174 return 0;
296 for (unsigned int j = 0; desc->valueNames[j]; ++j) { 296 for (unsigned int j = 0; desc->valueNames[j]; ++j) {
297 free((void *)desc->valueNames[j]); 297 free((void *)desc->valueNames[j]);
298 } 298 }
299 free((void *)desc->valueNames); 299 free((void *)desc->valueNames);
300 } 300 }
301 free((void *)desc);
301 } 302 }
302 free((void *)m_descriptor.parameters); 303 free((void *)m_descriptor.parameters);
303 304
304 for (unsigned int i = 0; i < m_descriptor.programCount; ++i) { 305 for (unsigned int i = 0; i < m_descriptor.programCount; ++i) {
305 free((void *)m_descriptor.programs[i]); 306 free((void *)m_descriptor.programs[i]);
601 if (m_fs.find(plugin) != m_fs.end()) { 602 if (m_fs.find(plugin) != m_fs.end()) {
602 size_t outputCount = 0; 603 size_t outputCount = 0;
603 if (m_pluginOutputs[plugin]) { 604 if (m_pluginOutputs[plugin]) {
604 outputCount = m_pluginOutputs[plugin]->size(); 605 outputCount = m_pluginOutputs[plugin]->size();
605 } 606 }
607 std::cerr << "cleanup: " << outputCount << " output(s)" << std::endl;
606 VampFeatureList *list = m_fs[plugin]; 608 VampFeatureList *list = m_fs[plugin];
607 for (unsigned int i = 0; i < outputCount; ++i) { 609 for (unsigned int i = 0; i < outputCount; ++i) {
608 for (unsigned int j = 0; j < m_fsizes[plugin][i]; ++j) { 610 for (unsigned int j = 0; j < m_fsizes[plugin][i]; ++j) {
609 if (list[i].features[j].v1.label) { 611 if (list[i].features[j].v1.label) {
610 free(list[i].features[j].v1.label); 612 free(list[i].features[j].v1.label);
613 free(list[i].features[j].v1.values); 615 free(list[i].features[j].v1.values);
614 } 616 }
615 } 617 }
616 if (list[i].features) free(list[i].features); 618 if (list[i].features) free(list[i].features);
617 } 619 }
620 if (list) free((void *)list);
618 m_fs.erase(plugin); 621 m_fs.erase(plugin);
619 m_fsizes.erase(plugin); 622 m_fsizes.erase(plugin);
620 m_fvsizes.erase(plugin); 623 m_fvsizes.erase(plugin);
621 } 624 }
622 625
847 } 850 }
848 851
849 void 852 void
850 PluginAdapterBase::Impl::resizeFS(Plugin *plugin, int n) 853 PluginAdapterBase::Impl::resizeFS(Plugin *plugin, int n)
851 { 854 {
852 // std::cerr << "PluginAdapterBase::Impl::resizeFS(" << plugin << ", " << n << ")" << std::endl; 855 #ifdef DEBUG_PLUGIN_ADAPTER
856 std::cerr << "PluginAdapterBase::Impl::resizeFS(" << plugin << ", " << n << ")" << std::endl;
857 #endif
853 858
854 int i = m_fsizes[plugin].size(); 859 int i = m_fsizes[plugin].size();
855 if (i >= n) return; 860 if (i >= n) return;
856 861
857 // std::cerr << "resizing from " << i << std::endl; 862 #ifdef DEBUG_PLUGIN_ADAPTER
863 std::cerr << "resizing from " << i << std::endl;
864 #endif
858 865
859 m_fs[plugin] = (VampFeatureList *)realloc 866 m_fs[plugin] = (VampFeatureList *)realloc
860 (m_fs[plugin], n * sizeof(VampFeatureList)); 867 (m_fs[plugin], n * sizeof(VampFeatureList));
861 868
862 while (i < n) { 869 while (i < n) {
869 } 876 }
870 877
871 void 878 void
872 PluginAdapterBase::Impl::resizeFL(Plugin *plugin, int n, size_t sz) 879 PluginAdapterBase::Impl::resizeFL(Plugin *plugin, int n, size_t sz)
873 { 880 {
874 // std::cerr << "PluginAdapterBase::Impl::resizeFL(" << plugin << ", " << n << ", " 881 #ifdef DEBUG_PLUGIN_ADAPTER
875 // << sz << ")" << std::endl; 882 std::cerr << "PluginAdapterBase::Impl::resizeFL(" << plugin << ", " << n << ", "
876 883 << sz << ")" << std::endl;
884 #endif
885
877 size_t i = m_fsizes[plugin][n]; 886 size_t i = m_fsizes[plugin][n];
878 if (i >= sz) return; 887 if (i >= sz) return;
879 888
880 // std::cerr << "resizing from " << i << std::endl; 889 #ifdef DEBUG_PLUGIN_ADAPTER
890 std::cerr << "resizing from " << i << std::endl;
891 #endif
881 892
882 m_fs[plugin][n].features = (VampFeatureUnion *)realloc 893 m_fs[plugin][n].features = (VampFeatureUnion *)realloc
883 (m_fs[plugin][n].features, 2 * sz * sizeof(VampFeatureUnion)); 894 (m_fs[plugin][n].features, 2 * sz * sizeof(VampFeatureUnion));
884 895
885 while (m_fsizes[plugin][n] < sz) { 896 while (m_fsizes[plugin][n] < sz) {
894 } 905 }
895 906
896 void 907 void
897 PluginAdapterBase::Impl::resizeFV(Plugin *plugin, int n, int j, size_t sz) 908 PluginAdapterBase::Impl::resizeFV(Plugin *plugin, int n, int j, size_t sz)
898 { 909 {
899 // std::cerr << "PluginAdapterBase::Impl::resizeFV(" << plugin << ", " << n << ", " 910 #ifdef DEBUG_PLUGIN_ADAPTER
900 // << j << ", " << sz << ")" << std::endl; 911 std::cerr << "PluginAdapterBase::Impl::resizeFV(" << plugin << ", " << n << ", "
901 912 << j << ", " << sz << ")" << std::endl;
913 #endif
914
902 size_t i = m_fvsizes[plugin][n][j]; 915 size_t i = m_fvsizes[plugin][n][j];
903 if (i >= sz) return; 916 if (i >= sz) return;
904 917
905 // std::cerr << "resizing from " << i << std::endl; 918 #ifdef DEBUG_PLUGIN_ADAPTER
906 919 std::cerr << "resizing from " << i << std::endl;
920 #endif
921
907 m_fs[plugin][n].features[j].v1.values = (float *)realloc 922 m_fs[plugin][n].features[j].v1.values = (float *)realloc
908 (m_fs[plugin][n].features[j].v1.values, sz * sizeof(float)); 923 (m_fs[plugin][n].features[j].v1.values, sz * sizeof(float));
909 924
910 m_fvsizes[plugin][n][j] = sz; 925 m_fvsizes[plugin][n][j] = sz;
911 } 926 }