zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 599eb9da3c406a9e0abb29a3abd0b87e4f8e2271
parent 99e83671d9719e209f817e8a942e4059efb31b09
Author: Hans Petter Selasky <[email protected]>
Date:   Thu,  5 Sep 2019 20:37:51 +0200

Throw a bad allocation exception when a note cannot be inserted into
note pool instead of asserting and killing ZynAddSubFX.

Signed-off-by: Hans Petter Selasky <[email protected]>

Diffstat:
Msrc/Containers/NotePool.cpp | 26++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/src/Containers/NotePool.cpp b/src/Containers/NotePool.cpp @@ -157,7 +157,18 @@ void NotePool::insertNote(note_t note, uint8_t sendto, SynthDescriptor desc, boo { //Get first free note descriptor int desc_id = getMergeableDescriptor(note, sendto, legato, ndesc); - assert(desc_id != -1); + int sdesc_id = 0; + if(desc_id < 0) + goto error; + + //Get first free synth descriptor + while(1) { + if (sdesc_id == POLYPHONY*EXPECTED_USAGE) + goto error; + if (sdesc[sdesc_id].note == 0) + break; + sdesc_id++; + } ndesc[desc_id].note = note; ndesc[desc_id].sendto = sendto; @@ -165,14 +176,13 @@ void NotePool::insertNote(note_t note, uint8_t sendto, SynthDescriptor desc, boo ndesc[desc_id].status = KEY_PLAYING; ndesc[desc_id].legatoMirror = legato; - //Get first free synth descriptor - int sdesc_id = 0; - while(sdesc[sdesc_id].note && sdesc_id < POLYPHONY*EXPECTED_USAGE) - sdesc_id++; - - assert(sdesc_id < POLYPHONY*EXPECTED_USAGE); - sdesc[sdesc_id] = desc; + return; +error: + //Avoid leaking note + desc.note->memory.dealloc(desc.note); + //Let caller handle failure + throw std::bad_alloc(); }; void NotePool::upgradeToLegato(void)