Bugfix: crash in some circumstances when avoid_crossing_perimeters is enabled. #2266