changeset 18:55969570044e

Fix crashes & valgrind warnings in plugin tester
author Chris Cannam
date Fri, 14 Oct 2011 10:44:38 +0100
parents 47e1917c88fc
children f66ed426a14f
files Agent.cpp Peaks.cpp Peaks.h
diffstat 3 files changed, 16 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Agent.cpp	Fri Oct 14 09:59:53 2011 +0100
+++ b/Agent.cpp	Fri Oct 14 10:44:38 2011 +0100
@@ -102,12 +102,16 @@
     EventList::iterator ei = events.begin();
     if (ei != events.end()) {
         EventList::iterator ni = ei;
-	prevBeat = (++ni)->time;
+        if (++ni != events.end()) {
+            prevBeat = ni->time;
+        }
     }
     while (ei != events.end()) {
         EventList::iterator ni = ei;
-        ++ni;
-        if (ni == events.end()) break;
+        if (ni == events.end() ||
+            ++ni == events.end()) {
+            break;
+        }
 	nextBeat = ni->time;
 	beats = nearbyint((nextBeat - prevBeat) / beatInterval - 0.01); //prefer slow
 	currentInterval = (nextBeat - prevBeat) / beats;
--- a/Peaks.cpp	Fri Oct 14 09:59:53 2011 +0100
+++ b/Peaks.cpp	Fri Oct 14 10:44:38 2011 +0100
@@ -58,6 +58,7 @@
     vector<int> peaks;
     int maxp = 0;
     int mid = 0;
+    if (data.empty()) return peaks;
     int end = data.size();
     double av = data[0];
     while (mid < end) {
--- a/Peaks.h	Fri Oct 14 09:59:53 2011 +0100
+++ b/Peaks.h	Fri Oct 14 10:44:38 2011 +0100
@@ -75,8 +75,14 @@
     static void getSlope(const vector<double> &data, double hop, int n,
 			 vector<double> &slope);
 
-    static double min(const vector<double> &arr) { return arr[imin(arr)]; }
-    static double max(const vector<double> &arr) { return arr[imax(arr)]; }
+    static double min(const vector<double> &arr) {
+        if (arr.empty()) return 0;
+        return arr[imin(arr)];
+    }
+    static double max(const vector<double> &arr) {
+        if (arr.empty()) return 0;
+        return arr[imax(arr)];
+    }
 
     static int imin(const vector<double> &arr);
     static int imax(const vector<double> &arr);