a more reliable/robust/less complex version of previous commit
authorPaul Davis <paul@linuxaudiosystems.com>
Thu, 21 Jul 2016 18:00:18 +0000 (14:00 -0400)
committerPaul Davis <paul@linuxaudiosystems.com>
Thu, 21 Jul 2016 18:00:18 +0000 (14:00 -0400)
libs/surfaces/generic_midi/generic_midi_control_protocol.cc
libs/surfaces/generic_midi/generic_midi_control_protocol.h

index 627ad8d66e9171a7728c93f3abda2fe9e2486850..d14a9ca24cac00bfae61d4c743dce1b4b7904d58 100644 (file)
@@ -328,8 +328,11 @@ GenericMidiControlProtocol::start_learning (Controllable* c)
                for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ) {
                        ptmp = i;
                        ++ptmp;
-                       if (((*i)->first)->get_controllable() == c) {
-                               (*i)->second.disconnect();
+                       if (((*i)->mc)->get_controllable() == c) {
+                               if ((*i)->own_mc) {
+                                       delete (*i)->mc;
+                               }
+                               (*i)->connection.disconnect();
                                delete *i;
                                pending_controllables.erase (i);
                        }
@@ -338,6 +341,7 @@ GenericMidiControlProtocol::start_learning (Controllable* c)
        }
 
        MIDIControllable* mc = 0;
+       bool own_mc = false;
 
        for (MIDIControllables::iterator i = controllables.begin(); i != controllables.end(); ++i) {
                if ((*i)->get_controllable() && ((*i)->get_controllable()->id() == c->id())) {
@@ -348,15 +352,14 @@ GenericMidiControlProtocol::start_learning (Controllable* c)
 
        if (!mc) {
                mc = new MIDIControllable (this, *_input_port->parser(), *c, false);
-               controllables.push_back (mc);
+               own_mc = true;
        }
 
        {
                Glib::Threads::Mutex::Lock lm (pending_lock);
 
-               MIDIPendingControllable* element = new MIDIPendingControllable;
-               element->first = mc;
-               c->LearningFinished.connect_same_thread (element->second, boost::bind (&GenericMidiControlProtocol::learning_stopped, this, mc));
+               MIDIPendingControllable* element = new MIDIPendingControllable (mc, own_mc);
+               c->LearningFinished.connect_same_thread (element->connection, boost::bind (&GenericMidiControlProtocol::learning_stopped, this, mc));
 
                pending_controllables.push_back (element);
        }
@@ -376,8 +379,8 @@ GenericMidiControlProtocol::learning_stopped (MIDIControllable* mc)
                tmp = i;
                ++tmp;
 
-               if ( (*i)->first == mc) {
-                       (*i)->second.disconnect();
+               if ( (*i)->mc == mc) {
+                       (*i)->connection.disconnect();
                        delete *i;
                        pending_controllables.erase(i);
                }
@@ -400,10 +403,10 @@ GenericMidiControlProtocol::stop_learning (Controllable* c)
        */
 
        for (MIDIPendingControllables::iterator i = pending_controllables.begin(); i != pending_controllables.end(); ++i) {
-               if (((*i)->first)->get_controllable() == c) {
-                       (*i)->first->stop_learning ();
-                       dptr = (*i)->first;
-                       (*i)->second.disconnect();
+               if (((*i)->mc)->get_controllable() == c) {
+                       (*i)->mc->stop_learning ();
+                       dptr = (*i)->mc;
+                       (*i)->connection.disconnect();
 
                        delete *i;
                        pending_controllables.erase (i);
index c1e59bc0dc1c5cd0088c665a02f6b6d26f22c5d2..d3709c5374d8a1af12525e871da5c27ddbcc72f8 100644 (file)
@@ -125,7 +125,16 @@ class GenericMidiControlProtocol : public ARDOUR::ControlProtocol {
        typedef std::list<MIDIAction*> MIDIActions;
        MIDIActions actions;
 
-       typedef std::pair<MIDIControllable*,PBD::ScopedConnection> MIDIPendingControllable;
+       struct MIDIPendingControllable {
+               MIDIControllable* mc;
+               bool own_mc;
+               PBD::ScopedConnection connection;
+
+               MIDIPendingControllable (MIDIControllable* c, bool omc)
+                       : mc (c)
+                       , own_mc (omc)
+               {}
+       };
        typedef std::list<MIDIPendingControllable* > MIDIPendingControllables;
        MIDIPendingControllables pending_controllables;
         Glib::Threads::Mutex controllables_lock;