Fix the crash when adding new scene during core event processing 24/208324/2
authorRichard Huang <r.huang@samsung.com>
Fri, 21 Jun 2019 13:14:16 +0000 (14:14 +0100)
committerRichard Huang <r.huang@samsung.com>
Tue, 25 Jun 2019 16:06:42 +0000 (17:06 +0100)
Change-Id: I6ed13af6305a3615f0c874a2112cb132d894d56d

automated-tests/src/dali/utc-Dali-Scene.cpp
dali/internal/common/core-impl.cpp
dali/internal/common/core-impl.h

index 21076e2..2b78fea 100644 (file)
@@ -91,12 +91,16 @@ struct KeyEventReceivedFunctor
 struct TouchedSignalData
 {
   TouchedSignalData()
-  : functorCalled(false)
+  : functorCalled(false),
+    createNewScene(false),
+    newSceneCreated(false)
   {}
 
   void Reset()
   {
     functorCalled = false;
+    createNewScene = false;
+    newSceneCreated = false;
 
     receivedTouchEvent.points.clear();
     receivedTouchEvent.time = 0;
@@ -105,6 +109,8 @@ struct TouchedSignalData
   }
 
   bool functorCalled;
+  bool createNewScene;
+  bool newSceneCreated;
   TouchEvent receivedTouchEvent;
   TouchData receivedTouchData;
 };
@@ -132,6 +138,14 @@ struct TouchFunctor
   {
     signalData.functorCalled = true;
     signalData.receivedTouchData = touch;
+
+    if ( signalData.createNewScene )
+    {
+      Dali::Integration::Scene scene = Dali::Integration::Scene::New( Vector2( 480.0f, 800.0f ) );
+      DALI_TEST_CHECK( scene );
+
+      signalData.newSceneCreated = true;
+    }
   }
 
   void operator()()
@@ -387,6 +401,31 @@ int UtcDaliSceneDiscard(void)
   END_TEST;
 }
 
+int UtcDaliSceneCreateNewSceneDuringCoreEventProcessing(void)
+{
+  TestApplication application;
+
+  Dali::Integration::Scene scene = application.GetScene();
+
+  TouchedSignalData data;
+  data.createNewScene = true;
+  TouchFunctor functor( data );
+  scene.TouchSignal().Connect( &application, functor );
+
+  // Render and notify.
+  application.SendNotification();
+  application.Render();
+
+  GenerateTouch( application, PointState::DOWN, Vector2( 10.0f, 10.0f ) );
+
+  DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
+  DALI_TEST_EQUALS( true, data.createNewScene, TEST_LOCATION );
+  DALI_TEST_EQUALS( true, data.newSceneCreated, TEST_LOCATION );
+  data.Reset();
+
+  END_TEST;
+}
+
 int UtcDaliSceneEventProcessingFinishedP(void)
 {
   TestApplication application;
index c1e7fde..8841da4 100644 (file)
@@ -268,18 +268,22 @@ void Core::ProcessEvents()
   // Signal that any messages received will be flushed soon
   mUpdateManager->EventProcessingStarted();
 
+  // Scene could be added or removed while processing the events
+  // Copy the Scene container locally to avoid possibly invalid iterator
+  SceneContainer scenes = mScenes;
+
   // process events in all scenes
-  for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
+  for( auto scene : scenes )
   {
-    (*iter)->ProcessEvents();
+    scene->ProcessEvents();
   }
 
   mNotificationManager->ProcessMessages();
 
   // Emit signal here to inform listeners that event processing has finished.
-  for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
+  for( auto scene : scenes )
   {
-    (*iter)->EmitEventProcessingFinishedSignal();
+    scene->EmitEventProcessingFinishedSignal();
   }
 
   // Run any registered processors
@@ -289,9 +293,9 @@ void Core::ProcessEvents()
   mRelayoutController->Relayout();
 
   // Rebuild depth tree after event processing has finished
-  for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
+  for( auto scene : scenes )
   {
-    (*iter)->RebuildDepthTree();
+    scene->RebuildDepthTree();
   }
 
   // Flush any queued messages for the update-thread
index 9105f7a..f596ae9 100644 (file)
@@ -326,7 +326,8 @@ private:
   OwnerPointer<GestureEventProcessor>           mGestureEventProcessor;       ///< The gesture event processor
   Dali::Vector<Integration::Processor*>         mProcessors;                  ///< Registered processors (not owned)
 
-  std::vector<ScenePtr>                         mScenes;                      ///< A container of scenes that bound to a surface for rendering, owned by Core
+  using SceneContainer = std::vector<ScenePtr>;
+  SceneContainer                                mScenes;                      ///< A container of scenes that bound to a surface for rendering, owned by Core
 
   // The object registry
   ObjectRegistryPtr                             mObjectRegistry;