From 5e4cb10f72ef69d73fbd2c1bc42465c9111a4c41 Mon Sep 17 00:00:00 2001
From: Bent Bisballe Nyeng <deva@aasimon.org>
Date: Thu, 12 Jan 2012 16:31:36 +0100
Subject: Fix memory leak when unloading a drumkit.

---
 src/audiofile.cc  |  4 +++-
 src/instrument.cc | 16 ++++++++++++++++
 src/instrument.h  |  3 ++-
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/audiofile.cc b/src/audiofile.cc
index 61b49e9..018b7fb 100644
--- a/src/audiofile.cc
+++ b/src/audiofile.cc
@@ -33,6 +33,7 @@
 
 AudioFile::AudioFile(std::string filename)
 {
+  //printf("new AudioFile %p\n", this);
   this->filename = filename;
 
   data = NULL;
@@ -43,6 +44,7 @@ AudioFile::AudioFile(std::string filename)
 
 AudioFile::~AudioFile()
 {
+  //printf("delete AudioFile %p\n", this);
   unload();
 }
 
@@ -62,7 +64,7 @@ void AudioFile::load()
   SF_INFO sf_info;
   SNDFILE *fh = sf_open(filename.c_str(), SFM_READ, &sf_info);
   if(!fh) {
-    printf("Load error...\n");
+    printf("SNDFILE Error (%s): %s\n", filename.c_str(), sf_strerror(fh));
     return;
   }
     
diff --git a/src/instrument.cc b/src/instrument.cc
index 8cfaec7..b10d990 100644
--- a/src/instrument.cc
+++ b/src/instrument.cc
@@ -34,12 +34,28 @@
 
 Instrument::Instrument()
 {
+  printf("new Instrument %p\n", this);
   mod = 1.0;
   lastpos = 0;
 }
 
+Instrument::~Instrument()
+{
+  printf("delete Instrument %p\n", this);
+  std::vector<AudioFile*>::iterator i = audiofiles.begin();
+  while(i != audiofiles.end()) {
+    delete *i;
+    i++;
+  }
+}
+
 Sample *Instrument::sample(level_t level, size_t pos)
 {
+  if(Conf::enable_velocity_modifier == false) {
+    mod = 1.0;
+    lastpos = 0;
+  }
+
   if(Conf::enable_velocity_randomiser) {
     float r = (float)rand() / (float)RAND_MAX; // random number: [0;1]
     r -= 0.5; // random number [-0.5;0.5]
diff --git a/src/instrument.h b/src/instrument.h
index e61d2b8..9b110be 100644
--- a/src/instrument.h
+++ b/src/instrument.h
@@ -39,6 +39,7 @@ class Instrument {
   friend class InstrumentParser;
 public:
   Instrument();
+  ~Instrument();
 
   Sample *sample(level_t level, size_t pos);
 
@@ -62,6 +63,6 @@ private:
 };
 
 //typedef std::map< std::string, Instrument > Instruments;
-typedef std::vector< Instrument > Instruments;
+typedef std::vector< Instrument* > Instruments;
 
 #endif/*__DRUMGIZMO_INSTRUMENT_H__*/
-- 
cgit v1.2.3