Why am I getting exception 0x80000003 when iterating over a set?

I have code that looks like this:

for (const FName& ActiveGroup : this->ActivatedWeightGroups)
{
	if (StartingWeightGroup.LexicalLess(ActiveGroup) && this->DeactivatePassiveGameplayEffects(ActiveGroup))
	{
		DeactivatedGroups.Add(ActiveGroup);
		this->ActivatedWeightGroups.Remove(ActiveGroup);
	}
}

For some reason, every time I run this code I get a very cryptic exception (0x80000003) in the debugger on the for line. Why? What causes this?

The issue here was that I was modifying the set while iterating over it, so the exception appears to have been that the code was reading data past the end of the set.

2 Likes

For anyone interested in how to remove items while iterating over a for loop, it’s advised you iterate in reverse.

for(int i = size - 1; i >= 0; i--)
{
// Remove index here
}
1 Like