Let range for loop don't touch container at RenderManager 45/266045/5
authorEunki, Hong <eunkiki.hong@samsung.com>
Thu, 4 Nov 2021 13:01:47 +0000 (22:01 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Fri, 19 Nov 2021 08:16:37 +0000 (17:16 +0900)
Reallocate or Resize the container during range-based-loop is UB.
So many case, we try to not add/erase some items during range-based-loop is working.

There was some codes that erase item during for loop.
So I make it out, for safety.

Change-Id: I919510200adfafbd95a6202ac295ffb7bc06c408
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali/internal/render/common/render-manager.cpp

index ce6852b848ad39b0a26da313c121eadeec5771d7..3c9f87c1fa22573280b44b5ba7a7a227b5e10469 100644 (file)
@@ -256,15 +256,13 @@ void RenderManager::RemoveTexture(Render::Texture* texture)
 {
   DALI_ASSERT_DEBUG(NULL != texture);
 
-  // Find the texture, use reference to pointer so we can do the erase safely
-  for(auto&& iter : mImpl->textureContainer)
+  // Find the texture, use std::find so we can do the erase safely
+  auto iter = std::find(mImpl->textureContainer.begin(), mImpl->textureContainer.end(), texture);
+
+  if(iter != mImpl->textureContainer.end())
   {
-    if(iter == texture)
-    {
-      texture->Destroy();
-      mImpl->textureContainer.Erase(&iter); // Texture found; now destroy it
-      return;
-    }
+    texture->Destroy();
+    mImpl->textureContainer.Erase(iter); // Texture found; now destroy it
   }
 }
 
@@ -302,16 +300,13 @@ void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
 {
   DALI_ASSERT_DEBUG(nullptr != frameBuffer);
 
-  // Find the sampler, use reference so we can safely do the erase
-  for(auto&& iter : mImpl->frameBufferContainer)
-  {
-    if(iter == frameBuffer)
-    {
-      frameBuffer->Destroy();
-      mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it
+  // Find the framebuffer, use std:find so we can safely do the erase
+  auto iter = std::find(mImpl->frameBufferContainer.begin(), mImpl->frameBufferContainer.end(), frameBuffer);
 
-      break;
-    }
+  if(iter != mImpl->frameBufferContainer.end())
+  {
+    frameBuffer->Destroy();
+    mImpl->frameBufferContainer.Erase(iter); // frameBuffer found; now destroy it
   }
 }
 
@@ -382,13 +377,11 @@ void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
 
 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
 {
-  auto it = std::find_if(mImpl->geometryContainer.begin(), mImpl->geometryContainer.end(), [geometry](auto& item) {
-    return geometry == item;
-  });
+  auto iter = std::find(mImpl->geometryContainer.begin(), mImpl->geometryContainer.end(), geometry);
 
-  if(it != mImpl->geometryContainer.end())
+  if(iter != mImpl->geometryContainer.end())
   {
-    mImpl->geometryContainer.Erase(it);
+    mImpl->geometryContainer.Erase(iter);
   }
 }