diff --git a/src/SIL.LCModel/DomainImpl/OverridesLing_Lex.cs b/src/SIL.LCModel/DomainImpl/OverridesLing_Lex.cs index 834c52fd..38eb5a11 100644 --- a/src/SIL.LCModel/DomainImpl/OverridesLing_Lex.cs +++ b/src/SIL.LCModel/DomainImpl/OverridesLing_Lex.cs @@ -7644,15 +7644,23 @@ protected override void RemoveObjectSideEffectsInternal(RemoveObjectEventArgs e) if (removedCtxt != null) { var featConstrs = GetFeatureConstraintsExcept(removedCtxt); - foreach (var constr in removedCtxt.PlusConstrRS) + foreach (var constr in removedCtxt.PlusConstrRS.ToList()) { if (!featConstrs.Contains(constr)) + { + // Remove constr from removedCtxt first to avoid deleting removedCtxt twice (fixes LT-21729). + removedCtxt.PlusConstrRS.Remove(constr); m_cache.LanguageProject.PhonologicalDataOA.FeatConstraintsOS.Remove(constr); + } } - foreach (var constr in removedCtxt.MinusConstrRS) + foreach (var constr in removedCtxt.MinusConstrRS.ToList()) { if (!featConstrs.Contains(constr)) + { + // Remove constr from removedCtxt first to avoid deleting removedCtxt twice. + removedCtxt.MinusConstrRS.Remove(constr); m_cache.LanguageProject.PhonologicalDataOA.FeatConstraintsOS.Remove(constr); + } } } }