--- linuxsampler/trunk/src/engines/EngineChannelBase.h 2014/06/09 19:20:37 2611 +++ linuxsampler/trunk/src/engines/EngineChannelBase.h 2014/06/10 13:32:16 2612 @@ -85,9 +85,11 @@ } virtual void DeleteRegionsInUse() { + RTList* previous = NULL; // prevent double free { InstrumentChangeCmd& cmd = InstrumentChangeCommand.GetConfigForUpdate(); if (cmd.pRegionsInUse) { + previous = cmd.pRegionsInUse; delete cmd.pRegionsInUse; cmd.pRegionsInUse = NULL; } @@ -96,7 +98,8 @@ { InstrumentChangeCmd& cmd = InstrumentChangeCommand.SwitchConfig(); if (cmd.pRegionsInUse) { - delete cmd.pRegionsInUse; + if (cmd.pRegionsInUse != previous) + delete cmd.pRegionsInUse; cmd.pRegionsInUse = NULL; } cmd.bChangeInstrument = false; @@ -180,6 +183,7 @@ ResetInternal(); DeleteRegionsInUse(); + UnloadScriptInUse(); InstrumentChangeCmd& cmd = InstrumentChangeCommand.GetConfigForUpdate(); if (cmd.pInstrument) { @@ -303,13 +307,22 @@ } virtual ~EngineChannelBase() { + InstrumentScript* previous = NULL; // prevent double free { InstrumentChangeCmd& cmd = InstrumentChangeCommand.GetConfigForUpdate(); - if (cmd.pScript) delete cmd.pScript; + if (cmd.pScript) { + previous = cmd.pScript; + delete cmd.pScript; + cmd.pScript = NULL; + } } { InstrumentChangeCmd& cmd = InstrumentChangeCommand.SwitchConfig(); - if (cmd.pScript) delete cmd.pScript; + if (cmd.pScript) { + if (previous != cmd.pScript) + delete cmd.pScript; + cmd.pScript = NULL; + } } } @@ -364,6 +377,22 @@ } /** + * Unload the currently used and loaded real-time instrument script. + * The source code of the script is retained, so that it can still + * be reloaded. + */ + void UnloadScriptInUse() { + { + InstrumentChangeCmd& cmd = InstrumentChangeCommand.GetConfigForUpdate(); + if (cmd.pScript) pScript->unload(); + } + { + InstrumentChangeCmd& cmd = InstrumentChangeCommand.SwitchConfig(); + if (cmd.pScript) pScript->unload(); + } + } + + /** * Load real-time instrument script and all its resources required * for the upcoming instrument change. * @@ -371,8 +400,6 @@ */ void LoadInstrumentScript(const String& text) { InstrumentChangeCmd& cmd = InstrumentChangeCommand.GetConfigForUpdate(); - // unload *PRE*-previous script - cmd.pScript->reset(); //TODO: previous script should be freed as soon as the EngineBase switched the instrument, right now 2 scripts are kept in memory all the time, even though the old one is not used anymore // load the new script cmd.pScript->load(text); }