Index: source/simulation2/system/ComponentManager.cpp =================================================================== --- source/simulation2/system/ComponentManager.cpp +++ source/simulation2/system/ComponentManager.cpp @@ -909,10 +909,6 @@ std::vector queue; queue.swap(m_DestructionQueue); - // Flatten all the dynamic subscriptions to ensure there are no dangling - // references in the 'removed' lists to components we're going to delete - FlattenDynamicSubscriptions(); - for (std::vector::iterator it = queue.begin(); it != queue.end(); ++it) { entity_id_t ent = *it; @@ -921,6 +917,10 @@ CMessageDestroy msg(ent); PostMessage(ent, msg); + // Some components may have dynamically unsubscribed following the Destroy message. + // We must re-flatten the arrays to avoid dangling pointers in the m_Added or m_Removed caches. + FlattenDynamicSubscriptions(); + // Destroy the components, and remove from m_ComponentsByTypeId: std::map >::iterator iit = m_ComponentsByTypeId.begin(); for (; iit != m_ComponentsByTypeId.end(); ++iit)