diff OrthancStone/Sources/Volumes/VolumeSceneLayerSource.cpp @ 1685:7896aac14587

Protected against usage of dying viewport
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 25 Nov 2020 12:16:53 +0100
parents 52b8b96cb55f
children 9ac2a65d4172
line wrap: on
line diff
--- a/OrthancStone/Sources/Volumes/VolumeSceneLayerSource.cpp	Wed Nov 25 12:16:33 2020 +0100
+++ b/OrthancStone/Sources/Volumes/VolumeSceneLayerSource.cpp	Wed Nov 25 12:16:53 2020 +0100
@@ -137,59 +137,62 @@
   void VolumeSceneLayerSource::Update(const CoordinateSystem3D& plane)
   {
     std::unique_ptr<IViewport::ILock> lock(GetViewportLock());
-    ViewportController& controller = lock->GetController();
-    Scene2D& scene = controller.GetScene();
-
-    assert(slicer_.get() != NULL);
-    std::unique_ptr<IVolumeSlicer::IExtractedSlice> slice(slicer_->ExtractSlice(plane));
-
-    if (slice.get() == NULL)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);        
-    }
-
-    if (!slice->IsValid())
-    {
-      // The slicer cannot handle this cutting plane: Clear the layer
-      ClearLayer();
-    }
-    else if (lastPlane_.get() != NULL &&
-             IsSameCuttingPlane(*lastPlane_, plane) &&
-             lastRevision_ == slice->GetRevision())
+    if(lock)
     {
-      // The content of the slice has not changed: Don't update the
-      // layer content, but possibly update its style
+      ViewportController& controller = lock->GetController();
+      Scene2D& scene = controller.GetScene();
 
-      if (configurator_.get() != NULL &&
-          configurator_->GetRevision() != lastConfiguratorRevision_ &&
-          scene.HasLayer(layerDepth_))
+      assert(slicer_.get() != NULL);
+      std::unique_ptr<IVolumeSlicer::IExtractedSlice> slice(slicer_->ExtractSlice(plane));
+
+      if (slice.get() == NULL)
       {
-        configurator_->ApplyStyle(scene.GetLayer(layerDepth_));
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);        
       }
-    }
-    else
-    {
-      LOG(TRACE) << "VolumeSceneLayerSource::Update -- Content has changed: An update is needed";
-      // Content has changed: An update is needed
-      lastPlane_.reset(new CoordinateSystem3D(plane));
-      lastRevision_ = slice->GetRevision();
 
-      std::unique_ptr<ISceneLayer> layer(slice->CreateSceneLayer(configurator_.get(), plane));
-      if (layer.get() == NULL)
+      if (!slice->IsValid())
+      {
+        // The slicer cannot handle this cutting plane: Clear the layer
+        ClearLayer();
+      }
+      else if (lastPlane_.get() != NULL &&
+              IsSameCuttingPlane(*lastPlane_, plane) &&
+              lastRevision_ == slice->GetRevision())
       {
-        LOG(TRACE) << "VolumeSceneLayerSource::Update -- (layer.get() == NULL)";
-        ClearLayer();
+        // The content of the slice has not changed: Don't update the
+        // layer content, but possibly update its style
+
+        if (configurator_.get() != NULL &&
+            configurator_->GetRevision() != lastConfiguratorRevision_ &&
+            scene.HasLayer(layerDepth_))
+        {
+          configurator_->ApplyStyle(scene.GetLayer(layerDepth_));
+        }
       }
       else
       {
-        LOG(TRACE) << "VolumeSceneLayerSource::Update -- (layer.get() != NULL)";
-        if (configurator_.get() != NULL)
+        LOG(TRACE) << "VolumeSceneLayerSource::Update -- Content has changed: An update is needed";
+        // Content has changed: An update is needed
+        lastPlane_.reset(new CoordinateSystem3D(plane));
+        lastRevision_ = slice->GetRevision();
+
+        std::unique_ptr<ISceneLayer> layer(slice->CreateSceneLayer(configurator_.get(), plane));
+        if (layer.get() == NULL)
         {
-          lastConfiguratorRevision_ = configurator_->GetRevision();
-          configurator_->ApplyStyle(*layer);
+          LOG(TRACE) << "VolumeSceneLayerSource::Update -- (layer.get() == NULL)";
+          ClearLayer();
         }
+        else
+        {
+          LOG(TRACE) << "VolumeSceneLayerSource::Update -- (layer.get() != NULL)";
+          if (configurator_.get() != NULL)
+          {
+            lastConfiguratorRevision_ = configurator_->GetRevision();
+            configurator_->ApplyStyle(*layer);
+          }
 
-        scene.SetLayer(layerDepth_, layer.release());
+          scene.SetLayer(layerDepth_, layer.release());
+        }
       }
     }
   }