I have a problem where there is a collection of objects (active animation class), inside a main object, which I'm iterating through it's 'ToList()' version on every interval.
I'm actually running an UpdateAnimations method, recursively, on the main item and all of it's sub items.
Sometimes I'm getting a "Object reference not set to an instance of an object" exception on one of the list's items. I really don't know why it happens...
I'm NOT inserting a null object to the original list and all of the actions that are made on the list - are made with a lock structure. I only have a few references to it, such as: Add, Remove and looping through it's 'ToList()' version.
These are all of my references of the problematic list (m_ActiveAnimations):
public void UpdateAnimations(double renderTime)
{
lock (m_SubElements)
{
// Execute child animations (order is not important)
foreach (var subElement in m_SubElements.ToList())
subElement.UpdateAnimations(renderTime);
}
lock (m_ActiveAnimations)
{
// Execute local active animations
foreach (var activeSeq in m_ActiveAnimations.ToList())
{
// Check if needs to be activated
if (double.IsNaN(activeSeq.StartTime))
{
// Start animation
activeSeq.StartTime = renderTime;
}
...
// Remove animation from active list
m_ActiveAnimations.Remove(activeSeq);
}
}
...
}
The adding part:
m_ActiveAnimations.Add(new ActiveAnimation());
And it throws here:
if (double.IsNaN(activeSeq.StartTime))
{
...
}