From 7091d997b33db1d7c9c1a0e237bea973c093ad09 Mon Sep 17 00:00:00 2001 From: paulfd Date: Sun, 25 Aug 2019 14:26:20 +0200 Subject: [PATCH] Corrected the segfault upon launching --- sources/Synth.h | 28 ++++++++++++++++------------ sources/Voice.h | 10 +++++----- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/sources/Synth.h b/sources/Synth.h index 15fb676b..ebbe245b 100644 --- a/sources/Synth.h +++ b/sources/Synth.h @@ -22,6 +22,7 @@ public: { for (int i = 0; i < config::numVoices; ++i) voices.push_back(std::make_unique(ccState)); + garbageCollectionThread = std::thread(&Synth::garbageCollection, this); } ~Synth() @@ -142,6 +143,20 @@ private: return freeVoice->get(); } + void garbageCollection() + { + while (!threadsShouldQuit) { + auto activeVoices { 0 }; + for (auto& voice : voices) { + voice->garbageCollect(); + if (!voice->isFree()) + activeVoices++; + } + DBG("Active voices:" << activeVoices << " | Stray buffers: " << FilePool::getFileBuffers()); + std::this_thread::sleep_for(1s); + } + } + bool hasGlobal { false }; bool hasControl { false }; int numGroups { 0 }; @@ -174,18 +189,7 @@ private: std::uniform_real_distribution randomDistribution { 0, 1 }; bool threadsShouldQuit { false }; - std::thread garbageCollectionThread { [&]() { - while (!threadsShouldQuit) { - auto activeVoices { 0 }; - for (auto& voice : voices) { - voice->garbageCollect(); - if (!voice->isFree()) - activeVoices++; - } - DBG("Active voices:" << activeVoices << " | Stray buffers: " << FilePool::getFileBuffers()); - std::this_thread::sleep_for(1s); - } - } }; + std::thread garbageCollectionThread; float getUniform() { diff --git a/sources/Voice.h b/sources/Voice.h index f7f787c0..e11ae666 100644 --- a/sources/Voice.h +++ b/sources/Voice.h @@ -217,14 +217,14 @@ public: } private: - Region* region; + Region* region { nullptr }; enum class State { idle, playing, release }; - State state; + State state { State::idle }; bool noteIsOff { false }; TriggerType triggerType; @@ -237,11 +237,11 @@ private: float baseGain { 1.0 }; float baseFrequency { 440.0 }; - uint32_t sourcePosition; - uint32_t initialDelay; + uint32_t sourcePosition { 0 }; + uint32_t initialDelay { 0 }; std::atomic dataReady { false }; - std::unique_ptr, std::function*)>> fileData; + std::unique_ptr, std::function*)>> fileData { nullptr }; Buffer tempBuffer1; Buffer tempBuffer2;