[Bugfix] entity.die() during iteration over grid.entities invalidates list iterator #273
Labels
No labels
Alpha Release Requirement
Bugfix
Demo Target
Documentation
Major Feature
Minor Feature
priority:tier1-active
priority:tier2-foundation
priority:tier3-future
priority:tier4-deferred
Refactoring & Cleanup
system:animation
system:documentation
system:grid
system:input
system:performance
system:procgen
system:python-binding
system:rendering
system:ui-hierarchy
Tiny Feature
workflow:blocked
workflow:needs-benchmark
workflow:needs-documentation
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
john/McRogueFace#273
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Calling
entity.die()inside afor entity in grid.entitiesloop callsentities->erase(it)which invalidates the list iterator held by the Python iterator object. While theUIEntityCollectionIterdoes check for size changes, the iterator itself (self->currentandself->end) can become invalidated by the erase operation.Root Cause
UIEntity.cpp:761-788(die method):UIEntityCollectionIterstores iterators at construction time:For
std::list, erasing an element only invalidates iterators to the erased element. So ifdie()erases the current element (the one the Python loop just yielded),self->currentis fine (it was already advanced past the yielded element). But ifdie()erases a different element that happens to beself->endor the elementself->currentpoints to, the iterator is invalidated.The size check (
start_sizecomparison at line 27-31) will catch the size change and raiseRuntimeError: EntityCollection changed size during iteration. However, this is a confusing error for users callingentity.die()in a loop.Reproduction
Impact
The size check prevents actual undefined behavior (which is good), but makes it impossible to iterate and remove entities. Users must collect entities first:
Fix Options
die()documentation that it cannot be called during iterationdie()marks the entity for removal; actual removal happens after the iteration completesgrid.entities.remove_if(predicate)as a safe bulk removal APIOption 2 (deferred removal) is the most user-friendly but most complex to implement. Option 1 is the pragmatic short-term fix.
Severity
Medium — does not cause crashes (size check catches the modification), but causes confusing
RuntimeErrorfor a natural game pattern (iterating entities and removing dead ones).