Skip to content

Commit

Permalink
fix #279805: Changing instrument in the mixer crashes the application
Browse files Browse the repository at this point in the history
I don't know how it could work before.
Keep MidiPatches list in the Zerberus class to be aligned with Fluid architecture. Sync load/delete soundfonts with patches list updates. Now, it should work as it is done in Fluid.

Most probably I fixed a memory leak related to static QList of MidiPatch pointers created on demand and updated each call which led to dead pointers here and there and not clearing memory.
  • Loading branch information
anatoly-os committed Dec 20, 2018
1 parent 0932122 commit b8cddb9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mscore/mixerdetails.cpp
Expand Up @@ -170,7 +170,7 @@ void MixerDetails::updateFromTrack()
//Populate patch combo
patchCombo->blockSignals(true);
patchCombo->clear();
const QList<MidiPatch*> pl = synti->getPatchInfo();
const auto& pl = synti->getPatchInfo();
int patchIndex = 0;

for (const MidiPatch* p : pl) {
Expand Down
2 changes: 1 addition & 1 deletion mscore/parteditbase.cpp
Expand Up @@ -341,7 +341,7 @@ void PartEdit::drumsetToggled(bool val, bool syncControls)

part->undoChangeProperty(Pid::USE_DRUMSET, val);
patch->clear();
const QList<MidiPatch*> pl = synti->getPatchInfo();
const auto& pl = synti->getPatchInfo();
for (const MidiPatch* p : pl) {
if (p->drum == val)
patch->addItem(p->name, QVariant::fromValue<void*>((void*)p));
Expand Down
22 changes: 15 additions & 7 deletions zerberus/zerberus.cpp
Expand Up @@ -76,6 +76,8 @@ Zerberus::~Zerberus()
}
for (Channel* c : _channel)
delete c;

qDeleteAll(patches);
}

//---------------------------------------------------------
Expand Down Expand Up @@ -249,18 +251,16 @@ const char* Zerberus::name() const
// getPatchInfo
//---------------------------------------------------------

const QList<Ms::MidiPatch*>& Zerberus::getPatchInfo() const
void Zerberus::updatePatchList()
{
static QList<Ms::MidiPatch*> pl;
qDeleteAll(pl);
pl.clear();
qDeleteAll(patches);
patches.clear();
int idx = 0;
for (ZInstrument* i : instruments) {
Ms::MidiPatch* p = new Ms::MidiPatch { false, name(), 0, idx, i->name() };
pl.append(p);
patches.append(p);
++idx;
}
return pl;
}

//---------------------------------------------------------
Expand Down Expand Up @@ -296,6 +296,7 @@ bool Zerberus::loadSoundFonts(const QStringList& sl)
if (!loadInstrument(s))
return false;
}
updatePatchList();
return true;
}

Expand All @@ -309,6 +310,7 @@ bool Zerberus::removeSoundFonts(const QStringList& fileNames)
if (!removeSoundFont(QFileInfo(fileName).absoluteFilePath()))
return false;
}
updatePatchList();
return true;
}

Expand All @@ -331,7 +333,9 @@ QStringList Zerberus::soundFonts() const
bool Zerberus::addSoundFont(const QString& s)
{
QMutexLocker locker(&mutex);
return loadInstrument(s);
bool res = loadInstrument(s);
updatePatchList();
return res;
}

//---------------------------------------------------------
Expand Down Expand Up @@ -364,9 +368,13 @@ bool Zerberus::removeSoundFont(const QString& s)
globalInstruments.erase(it1);
delete i;
}

updatePatchList();
return true;
}
}

updatePatchList();
return false;
}

Expand Down
9 changes: 7 additions & 2 deletions zerberus/zerberus.h
Expand Up @@ -22,6 +22,7 @@

#include "synthesizer/synthesizer.h"
#include "synthesizer/event.h"
#include "synthesizer/midipatch.h"
#include "voice.h"


Expand Down Expand Up @@ -74,7 +75,8 @@ class VoiceFifo {
class Zerberus : public Ms::Synthesizer {
static bool initialized;
static std::list<ZInstrument*> globalInstruments;

QList<Ms::MidiPatch*> patches;

double _masterTuning = 440.0;
std::atomic<bool> busy;

Expand Down Expand Up @@ -117,7 +119,6 @@ class Zerberus : public Ms::Synthesizer {
double ct2hz(double c) const { return pow(2.0, (c-6900.0) / 1200.0) * masterTuning(); }

virtual const char* name() const;
virtual const QList<Ms::MidiPatch*>& getPatchInfo() const;

virtual Ms::SynthesizerGroup state() const;
virtual bool setState(const Ms::SynthesizerGroup&);
Expand All @@ -131,6 +132,10 @@ class Zerberus : public Ms::Synthesizer {
virtual bool removeSoundFonts(const QStringList& fileNames);
virtual QStringList soundFonts() const;

virtual const QList<Ms::MidiPatch*>& getPatchInfo() const override { return patches; }

void updatePatchList();

virtual Ms::SynthesizerGui* gui();
static QFileInfoList sfzFiles();
};
Expand Down

0 comments on commit b8cddb9

Please sign in to comment.