I received an error report which seems to come from the following code:
public class AnimationChannelCollection : ReadOnlyCollection<BoneKeyFrameCollection>
{
private Dictionary<string, BoneKeyFrameCollection> dict =
new Dictionary<string, BoneKeyFrameCollection>();
private ReadOnlyCollection<string> affectedBones;
// This immutable data structure should not be created by the library user
internal AnimationChannelCollection(IList<BoneKeyFrameCollection> channels)
: base(channels)
{
// Find the affected bones
List<string> affected = new List<string>();
foreach (BoneKeyFrameCollection frames in channels)
{
dict.Add(frames.BoneName, frames);
affected.Add(frames.BoneName);
}
affectedBones = new ReadOnlyCollection<string>(affected);
}
public BoneKeyFrameCollection this[string boneName]
{
get { return dict[boneName]; }
}
}
This is the calling code that reads the dictionary:
public override Matrix GetCurrentBoneTransform(BonePose pose)
{
BoneKeyFrameCollection channel = base.AnimationInfo.AnimationChannels[pose.Name];
}
This is the code that creates the dictionary, happens on startup:
// Reads in processed animation info written in the pipeline
internal sealed class AnimationReader : ContentTypeReader<AnimationInfoCollection>
{
/// <summary>
/// Reads in an XNB stream and converts it to a ModelInfo object
/// </summary>
/// <param name="input">The stream from which the data will be read</param>
/// <param name="existingInstance">Not used</param>
/// <returns>The unserialized ModelAnimationCollection object</returns>
protected override AnimationInfoCollection Read(ContentReader input, AnimationInfoCollection existingInstance)
{
AnimationInfoCollection dict = new AnimationInfoCollection();
int numAnimations = input.ReadInt32();
/* abbreviated */
AnimationInfo anim = new AnimationInfo(animationName, new AnimationChannelCollection(animList));
}
}
The error is:
Index was outside the bounds of the array.
Line: 0
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Xclna.Xna.Animation.InterpolationController.GetCurrentBoneTransform(BonePose pose)
I would have expected a KeyNotFoundException with the wrong key, but instead I get "Index was outside the bounds of the array". I don't understand how I could get that exception from the above code?
The code is singlethreaded by the way.
return dict[boneName];
– David LKeyNotFoundException
. You need to update your code to properly reproduce the issue as it currently does not. – David L