Skip to content

Commit 32b132d

Browse files
committed
Fix: Crash if far too many sections
Closes #557
1 parent 074dcc7 commit 32b132d

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

source/RouteManager2/CurrentRoute.cs

+24-4
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,42 @@ public CurrentRoute(BaseRenderer renderer)
131131
/// <summary>Updates all sections within the route</summary>
132132
public void UpdateAllSections()
133133
{
134-
UpdateSection(Sections.LastOrDefault());
134+
/*
135+
* When there are an insane amount of sections, updating via a reference chain
136+
* may trigger a StackOverflowException
137+
*
138+
* Instead, pull out the reference to the next section in an out variable
139+
* and use a while loop
140+
* https://github.com/leezer3/OpenBVE/issues/557
141+
*/
142+
Section nextSectionToUpdate;
143+
UpdateSection(Sections.LastOrDefault(), out nextSectionToUpdate);
144+
while (nextSectionToUpdate != null)
145+
{
146+
UpdateSection(nextSectionToUpdate, out nextSectionToUpdate);
147+
}
135148
}
136149

137150
/// <summary>Updates the specified signal section</summary>
138151
/// <param name="SectionIndex"></param>
139152
public void UpdateSection(int SectionIndex)
140153
{
141-
UpdateSection(Sections[SectionIndex]);
154+
Section nextSectionToUpdate;
155+
UpdateSection(Sections[SectionIndex], out nextSectionToUpdate);
156+
while (nextSectionToUpdate != null)
157+
{
158+
UpdateSection(nextSectionToUpdate, out nextSectionToUpdate);
159+
}
142160
}
143161

144162
/// <summary>Updates the specified signal section</summary>
145163
/// <param name="Section"></param>
146-
public void UpdateSection(Section Section)
164+
/// <param name="PreviousSection"></param>
165+
public void UpdateSection(Section Section, out Section PreviousSection)
147166
{
148167
if (Section == null)
149168
{
169+
PreviousSection = null;
150170
return;
151171
}
152172

@@ -355,7 +375,7 @@ public void UpdateSection(Section Section)
355375
Section.CurrentAspect = newAspect;
356376

357377
// update previous section
358-
UpdateSection(Section.PreviousSection);
378+
PreviousSection = Section.PreviousSection;
359379
}
360380

361381
/// <summary>Updates the currently displayed background</summary>

0 commit comments

Comments
 (0)