changeset 1755:fd7f127ecd89 by-id

Don't hold on to borrowed pointer around the loop - so as to be informed when it becomes obsolete
author Chris Cannam
date Fri, 05 Jul 2019 16:55:54 +0100
parents 8529763e2258
children 776389390473
files transform/FeatureExtractionModelTransformer.cpp transform/RealTimeEffectModelTransformer.cpp
diffstat 2 files changed, 51 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/transform/FeatureExtractionModelTransformer.cpp	Fri Jul 05 16:55:27 2019 +0100
+++ b/transform/FeatureExtractionModelTransformer.cpp	Fri Jul 05 16:55:54 2019 +0100
@@ -648,17 +648,29 @@
     if (m_abandoned) return;
 
     ModelId inputId = getInputModel();
-    auto input = ModelById::getAs<DenseTimeValueModel>(inputId);
-    if (!input) {
-        abandon();
-        return;
-    }
 
-    sv_samplerate_t sampleRate = input->getSampleRate();
+    sv_samplerate_t sampleRate;
+    int channelCount;
+    sv_frame_t startFrame;
+    sv_frame_t endFrame;
+    
+    { // scope so as not to have this borrowed pointer retained around
+      // the edges of the process loop
+        auto input = ModelById::getAs<DenseTimeValueModel>(inputId);
+        if (!input) {
+            abandon();
+            return;
+        }
 
-    int channelCount = input->getChannelCount();
-    if ((int)m_plugin->getMaxChannelCount() < channelCount) {
-        channelCount = 1;
+        sampleRate = input->getSampleRate();
+
+        channelCount = input->getChannelCount();
+        if ((int)m_plugin->getMaxChannelCount() < channelCount) {
+            channelCount = 1;
+        }
+
+        startFrame = input->getStartFrame();
+        endFrame = input->getEndFrame();
     }
 
     float **buffers = new float*[channelCount];
@@ -696,9 +708,6 @@
         }
     }
 
-    sv_frame_t startFrame = input->getStartFrame();
-    sv_frame_t endFrame = input->getEndFrame();
-
     RealTime contextStartRT = primaryTransform.getStartTime();
     RealTime contextDurationRT = primaryTransform.getDuration();
 
@@ -761,6 +770,11 @@
                 ((((blockFrame - contextStart) / stepSize) * 99) /
                  (contextDuration / stepSize + 1));
 
+            if (!ModelById::get(inputId)) {
+                abandon();
+                return;
+            }
+
             // channelCount is either input->channelCount or 1
 
             if (frequencyDomain) {
--- a/transform/RealTimeEffectModelTransformer.cpp	Fri Jul 05 16:55:27 2019 +0100
+++ b/transform/RealTimeEffectModelTransformer.cpp	Fri Jul 05 16:55:54 2019 +0100
@@ -142,6 +142,25 @@
         return;
     }
 
+    sv_samplerate_t sampleRate;
+    int channelCount;
+    sv_frame_t startFrame;
+    sv_frame_t endFrame;
+
+    { // scope so as not to have this borrowed pointer retained around
+      // the edges of the process loop
+        auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
+        if (!input) {
+            abandon();
+            return;
+        }
+
+        sampleRate = input->getSampleRate();
+        channelCount = input->getChannelCount();
+        startFrame = input->getStartFrame();
+        endFrame = input->getEndFrame();
+    }
+
     auto stvm = ModelById::getAs<SparseTimeValueModel>(m_outputs[0]);
     auto wwfm = ModelById::getAs<WritableWaveFileModel>(m_outputs[0]);
 
@@ -153,17 +172,12 @@
         return;
     }
 
-    sv_samplerate_t sampleRate = input->getSampleRate();
-    int channelCount = input->getChannelCount();
     if (!wwfm && m_input.getChannel() != -1) channelCount = 1;
 
     sv_frame_t blockSize = m_plugin->getBufferSize();
 
     float **inbufs = m_plugin->getAudioInputBuffers();
 
-    sv_frame_t startFrame = input->getStartFrame();
-    sv_frame_t endFrame = input->getEndFrame();
-
     Transform transform = m_transforms[0];
     
     RealTime contextStartRT = transform.getStartTime();
@@ -205,6 +219,12 @@
 
         sv_frame_t got = 0;
 
+        auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
+        if (!input) {
+            abandon();
+            return;
+        }
+
         if (channelCount == 1) {
             if (inbufs && inbufs[0]) {
                 auto data = input->getData