changeset 1685:7896aac14587

Protected against usage of dying viewport
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 25 Nov 2020 12:16:53 +0100
parents fb62c1522193
children 59fc37c10cfa cc5a2e076746
files OrthancStone/Sources/Scene2DViewport/LayerHolder.cpp OrthancStone/Sources/Scene2DViewport/MeasureTrackers.cpp OrthancStone/Sources/Volumes/VolumeSceneLayerSource.cpp
diffstat 3 files changed, 70 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancStone/Sources/Scene2DViewport/LayerHolder.cpp	Wed Nov 25 12:16:33 2020 +0100
+++ b/OrthancStone/Sources/Scene2DViewport/LayerHolder.cpp	Wed Nov 25 12:16:53 2020 +0100
@@ -96,15 +96,18 @@
   void LayerHolder::DeleteLayers()
   {
     std::unique_ptr<IViewport::ILock> lock(GetViewportLock());
-    Scene2D& scene = lock->GetController().GetScene();
-
-    for (int i = 0; i < textLayerCount_ + polylineLayerCount_; ++i)
+    if (lock)
     {
-      ORTHANC_ASSERT(scene.HasLayer(baseLayerIndex_ + i), "No layer");
-      scene.DeleteLayer(baseLayerIndex_ + i);
+      Scene2D& scene = lock->GetController().GetScene();
+
+      for (int i = 0; i < textLayerCount_ + polylineLayerCount_; ++i)
+      {
+        ORTHANC_ASSERT(scene.HasLayer(baseLayerIndex_ + i), "No layer");
+        scene.DeleteLayer(baseLayerIndex_ + i);
+      }
+      baseLayerIndex_ = -1;
+      lock->Invalidate();
     }
-    baseLayerIndex_ = -1;
-    lock->Invalidate();
   }
   
   PolylineSceneLayer* LayerHolder::GetPolylineLayer(int index /*= 0*/)
--- a/OrthancStone/Sources/Scene2DViewport/MeasureTrackers.cpp	Wed Nov 25 12:16:33 2020 +0100
+++ b/OrthancStone/Sources/Scene2DViewport/MeasureTrackers.cpp	Wed Nov 25 12:16:53 2020 +0100
@@ -60,12 +60,15 @@
 
     std::unique_ptr<IViewport::ILock> lock(GetViewportLock());
 
-    if (commitResult_)
-      lock->GetController().PushCommand(command_);
-    else
-      command_->Undo();
+    if(lock)
+    {
+      if (commitResult_)
+        lock->GetController().PushCommand(command_);
+      else
+        command_->Undo();
 
-    lock->Invalidate();
+      lock->Invalidate();
+    }
   }
 
   EditMeasureTracker::EditMeasureTracker(boost::weak_ptr<IViewport> viewport,
@@ -108,12 +111,15 @@
 
     std::unique_ptr<IViewport::ILock> lock(GetViewportLock());
 
-    if (commitResult_)
-      lock->GetController().PushCommand(command_);
-    else
-      command_->Undo();
+    if(lock)
+    {
+      if (commitResult_)
+        lock->GetController().PushCommand(command_);
+      else
+        command_->Undo();
 
-    lock->Invalidate();
+      lock->Invalidate();
+    }
   }
 }
 
--- 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());
+        }
       }
     }
   }