From 1082f7cd122a728edbeae79607a61091a0dc494c Mon Sep 17 00:00:00 2001 From: tslater2006 Date: Wed, 6 Oct 2021 13:30:04 -0500 Subject: [PATCH] Dedup contours during merging of resinTrapGroups. Fix for edge case where a contour is in multiple groups and they get merged. Doesn't fix root cause of the contour being placed in 2 groups, but for the time being this works around it be deduping during the merge --- UVtools.Core/Managers/IssueManager.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/UVtools.Core/Managers/IssueManager.cs b/UVtools.Core/Managers/IssueManager.cs index 515b492..95831e8 100644 --- a/UVtools.Core/Managers/IssueManager.cs +++ b/UVtools.Core/Managers/IssueManager.cs @@ -871,10 +871,23 @@ namespace UVtools.Core.Managers else { var combinedGroup = new List<(VectorOfVectorOfPoint contour, uint layerIndex)>(); + + /* for reasons not fully understood, in rare cases you can end up with the same contour in multiple groups that are + * about to merge. This can cause a use-after-free type error later on when we dispose of a contour. + * We can (at least in the short term) remedy this by de-duping the result of the merge */ + var uniqueCombinedGroup = new HashSet<(VectorOfVectorOfPoint contour, uint layerIndex)>(); foreach (var index in overlappingGroupIndexes) { - combinedGroup.AddRange(resinTrapGroups[index]); + foreach (var p in resinTrapGroups[index]) + { + uniqueCombinedGroup.Add(p); + } } + combinedGroup.AddRange(uniqueCombinedGroup.ToList()); + combinedGroup.Add((resinTraps[layerIndex][x], (uint)layerIndex)); + + uniqueCombinedGroup.Clear(); + uniqueCombinedGroup = null; for (var index = overlappingGroupIndexes.Count - 1; index >= 0; index--) { @@ -882,7 +895,6 @@ namespace UVtools.Core.Managers resinTrapGroups.RemoveAt(index); } - combinedGroup.Add((resinTraps[layerIndex][x], (uint)layerIndex)); resinTrapGroups.Add(combinedGroup); } }