@@ -90,8 +90,6 @@ namespace tsom
9090 {
9191 for (auto it = m_updateJobs.begin (); it != m_updateJobs.end (); ++it)
9292 {
93- // FIXME: If applyFunc inserts a new job it will invalidate the iterators
94-
9593 UpdateJob& job = *it->second ;
9694 if (!job.HasFinished ())
9795 continue ;
@@ -112,15 +110,16 @@ namespace tsom
112110
113111 if (canExecute)
114112 {
115- job.applyFunc (it->first , std::move (job));
116-
117- // Don't remove jobs immediatly to be able to detect dependencies errors
118- m_finishedJobs.push_back (it->first );
113+ // Don't remove jobs immediately to be able to detect dependencies errors
114+ m_finishedJobs.emplace_back (FinishedJob{ it->first , it->second });
119115 }
120116 }
121117
122- for (const ChunkIndices& indices : m_finishedJobs)
118+ for (auto && [indices, job] : m_finishedJobs)
119+ {
120+ job->applyFunc (indices, std::move (*job));
123121 m_updateJobs.erase (indices);
122+ }
124123 m_finishedJobs.clear ();
125124
126125 for (auto && [chunkIndices, neighborMask] : m_invalidatedChunks)
@@ -131,6 +130,7 @@ namespace tsom
131130
132131 void ChunkEntities::CreateChunkEntity (const ChunkIndices& chunkIndices, Chunk& chunk)
133132 {
133+ std::unique_lock chunkLock (m_chunkLock);
134134 entt::handle chunkEntity = m_world.CreateEntity ();
135135
136136 auto & nodeComponent = chunkEntity.emplace <Nz::NodeComponent>(m_chunkContainer.GetChunkOffset (chunkIndices));
@@ -168,6 +168,7 @@ namespace tsom
168168
169169 void ChunkEntities::DestroyChunkEntity (const ChunkIndices& chunkIndices)
170170 {
171+ std::unique_lock chunkLock (m_chunkLock);
171172 if (auto it = m_updateJobs.find (chunkIndices); it != m_updateJobs.end ())
172173 {
173174 UpdateJob& job = *it->second ;
0 commit comments