comparison vamp-sdk/PluginAdapter.cpp @ 7:c66551966b5f

* Fix a couple of memory management and corruption bugs * Some textual fixes
author cannam
date Fri, 31 Mar 2006 17:39:49 +0000
parents 8f10d35a4090
children 44113b1e296b
comparison
equal deleted inserted replaced
6:8f10d35a4090 7:c66551966b5f
60 m_descriptor.copyright = strdup(plugin->getCopyright().c_str()); 60 m_descriptor.copyright = strdup(plugin->getCopyright().c_str());
61 61
62 m_descriptor.parameterCount = m_parameters.size(); 62 m_descriptor.parameterCount = m_parameters.size();
63 m_descriptor.parameters = (const VampParameterDescriptor **) 63 m_descriptor.parameters = (const VampParameterDescriptor **)
64 malloc(m_parameters.size() * sizeof(VampParameterDescriptor)); 64 malloc(m_parameters.size() * sizeof(VampParameterDescriptor));
65 65
66 for (unsigned int i = 0; i < m_parameters.size(); ++i) { 66 unsigned int i;
67
68 for (i = 0; i < m_parameters.size(); ++i) {
67 VampParameterDescriptor *desc = (VampParameterDescriptor *) 69 VampParameterDescriptor *desc = (VampParameterDescriptor *)
68 malloc(sizeof(VampParameterDescriptor)); 70 malloc(sizeof(VampParameterDescriptor));
69 desc->name = strdup(m_parameters[i].name.c_str()); 71 desc->name = strdup(m_parameters[i].name.c_str());
70 desc->description = strdup(m_parameters[i].description.c_str()); 72 desc->description = strdup(m_parameters[i].description.c_str());
71 desc->unit = strdup(m_parameters[i].unit.c_str()); 73 desc->unit = strdup(m_parameters[i].unit.c_str());
79 81
80 m_descriptor.programCount = m_programs.size(); 82 m_descriptor.programCount = m_programs.size();
81 m_descriptor.programs = (const char **) 83 m_descriptor.programs = (const char **)
82 malloc(m_programs.size() * sizeof(const char *)); 84 malloc(m_programs.size() * sizeof(const char *));
83 85
84 for (unsigned int i = 0; i < m_programs.size(); ++i) { 86 for (i = 0; i < m_programs.size(); ++i) {
85 m_descriptor.programs[i] = strdup(m_programs[i].c_str()); 87 m_descriptor.programs[i] = strdup(m_programs[i].c_str());
86 } 88 }
87 89
88 if (plugin->getInputDomain() == Plugin::FrequencyDomain) { 90 if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
89 m_descriptor.inputDomain = vampFrequencyDomain; 91 m_descriptor.inputDomain = vampFrequencyDomain;
314 316
315 void 317 void
316 PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList **fs) 318 PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList **fs)
317 { 319 {
318 if (!fs) return; 320 if (!fs) return;
321
319 for (unsigned int i = 0; fs[i]; ++i) { 322 for (unsigned int i = 0; fs[i]; ++i) {
323
320 for (unsigned int j = 0; j < fs[i]->featureCount; ++j) { 324 for (unsigned int j = 0; j < fs[i]->featureCount; ++j) {
321 VampFeature *feature = &fs[i]->features[j]; 325 VampFeature *feature = &fs[i]->features[j];
322 if (feature->values) free((void *)feature->values); 326 if (feature->values) free((void *)feature->values);
323 if (feature->label) free((void *)feature->label); 327 if (feature->label) free((void *)feature->label);
324 free((void *)feature);
325 } 328 }
329
326 if (fs[i]->features) free((void *)fs[i]->features); 330 if (fs[i]->features) free((void *)fs[i]->features);
327 free((void *)fs[i]); 331 free((void *)fs[i]);
328 } 332 }
333
329 free((void *)fs); 334 free((void *)fs);
330 } 335 }
331 336
332 void 337 void
333 PluginAdapterBase::cleanup(Plugin *plugin) 338 PluginAdapterBase::cleanup(Plugin *plugin)
371 desc->description = strdup(od.description.c_str()); 376 desc->description = strdup(od.description.c_str());
372 desc->unit = strdup(od.unit.c_str()); 377 desc->unit = strdup(od.unit.c_str());
373 desc->hasFixedValueCount = od.hasFixedValueCount; 378 desc->hasFixedValueCount = od.hasFixedValueCount;
374 desc->valueCount = od.valueCount; 379 desc->valueCount = od.valueCount;
375 380
376 desc->valueNames = (const char **) 381 if (od.valueCount > 0) {
377 malloc(od.valueCount * sizeof(const char *)); 382 desc->valueNames = (const char **)
383 malloc(od.valueCount * sizeof(const char *));
378 384
379 for (unsigned int i = 0; i < od.valueCount; ++i) { 385 for (unsigned int i = 0; i < od.valueCount; ++i) {
380 if (i < od.valueNames.size()) { 386 if (i < od.valueNames.size()) {
381 desc->valueNames[i] = strdup(od.valueNames[i].c_str()); 387 desc->valueNames[i] = strdup(od.valueNames[i].c_str());
382 } else { 388 } else {
383 desc->valueNames[i] = 0; 389 desc->valueNames[i] = 0;
390 }
384 } 391 }
392 } else {
393 desc->valueNames = 0;
385 } 394 }
386 395
387 desc->hasKnownExtents = od.hasKnownExtents; 396 desc->hasKnownExtents = od.hasKnownExtents;
388 desc->minValue = od.minValue; 397 desc->minValue = od.minValue;
389 desc->maxValue = od.maxValue; 398 desc->maxValue = od.maxValue;
433 442
434 VampFeatureList **fs = (VampFeatureList **) 443 VampFeatureList **fs = (VampFeatureList **)
435 malloc((n + 1) * sizeof(VampFeatureList *)); 444 malloc((n + 1) * sizeof(VampFeatureList *));
436 445
437 for (unsigned int i = 0; i < n; ++i) { 446 for (unsigned int i = 0; i < n; ++i) {
447
438 fs[i] = (VampFeatureList *)malloc(sizeof(VampFeatureList)); 448 fs[i] = (VampFeatureList *)malloc(sizeof(VampFeatureList));
449
439 if (features.find(i) == features.end()) { 450 if (features.find(i) == features.end()) {
451
440 fs[i]->featureCount = 0; 452 fs[i]->featureCount = 0;
441 fs[i]->features = 0; 453 fs[i]->features = 0;
442 } else { 454 continue;
443 Plugin::FeatureSet::const_iterator fi = 455 }
444 features.find(i); 456
445 const Plugin::FeatureList &fl = fi->second; 457 Plugin::FeatureSet::const_iterator fi = features.find(i);
446 fs[i]->featureCount = fl.size(); 458
447 fs[i]->features = (VampFeature *)malloc(fl.size() * 459 const Plugin::FeatureList &fl = fi->second;
448 sizeof(VampFeature)); 460
449 for (unsigned int j = 0; j < fl.size(); ++j) { 461 fs[i]->featureCount = fl.size();
450 fs[i]->features[j].hasTimestamp = fl[j].hasTimestamp; 462
451 fs[i]->features[j].sec = fl[j].timestamp.sec; 463 if (fs[i]->featureCount == 0) {
452 fs[i]->features[j].nsec = fl[j].timestamp.nsec; 464 fs[i]->features = 0;
453 fs[i]->features[j].valueCount = fl[j].values.size(); 465 continue;
454 fs[i]->features[j].values = (float *)malloc 466 }
455 (fs[i]->features[j].valueCount * sizeof(float)); 467
456 for (unsigned int k = 0; k < fs[i]->features[j].valueCount; ++k) { 468 fs[i]->features = (VampFeature *)malloc(fl.size() * sizeof(VampFeature));
457 fs[i]->features[j].values[k] = fl[j].values[k]; 469
458 } 470 for (unsigned int j = 0; j < fl.size(); ++j) {
459 fs[i]->features[j].label = strdup(fl[j].label.c_str()); 471
472 VampFeature *feature = &fs[i]->features[j];
473
474 feature->hasTimestamp = fl[j].hasTimestamp;
475 feature->sec = fl[j].timestamp.sec;
476 feature->nsec = fl[j].timestamp.nsec;
477 feature->valueCount = fl[j].values.size();
478 feature->label = strdup(fl[j].label.c_str());
479
480 if (feature->valueCount == 0) {
481 feature->values = 0;
482 continue;
483 }
484
485 feature->values = (float *)malloc
486 (feature->valueCount * sizeof(float));
487
488 for (unsigned int k = 0; k < feature->valueCount; ++k) {
489 feature->values[k] = fl[j].values[k];
460 } 490 }
461 } 491 }
462 } 492 }
463 493
464 fs[n] = 0; 494 fs[n] = 0;