Fix Gazebo crash in building editor when adding door or window (#2276) #3129
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BuildingMaker::DetachFromParent: break loop after DetachAllChildren.
Prevents memory corruption by iterating over attachmentMap while removing elements.
How to reproduce
The cause
Backtrace from
gdb
indicates a segmentation fault duringBuildingMaker::DetachFromParent
.The
attachmentMap
is amap<string, vector<string>>>
, for each wall (e.g. 'Wall_0') it holds the attached doors and windows (e.g. 'Door_0'). When adding for instance a new door to a wall, the item is added to theattachmentMap
when the cursor enters the wall. When the cursor moves out of the wall, the item must be detached from the wall.When the item to be detached is the last attached item for the wall, the wall itself must be removed from the
attachmentMap
. That is done by the call toDetachAllChildren
.The problem is that after
DetachAllChildren
removes the parent wall from theattachmentMap
,DetachFromParent
keeps iterating. Iterating over a collection while deleting elements is bad.My fix
Since a door or window can have only one parent, it makes sense to quit the loop after we have removed the childless parent from the map, by adding a
break
afterDetachAllChildren
.