4
Vote

Index was out of range

description

I received an error working in as .ascx file (haven't seen the error in other filetypes - yet) notifying me that an extension caused the erorr. As instructed by the message box, I turned on VS logging to ActivityLog.xml and here is the error I received in the log:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List1.get_Item(Int32 index) at System.Collections.ObjectModel.ReadOnlyCollection1.System.Collections.Generic.IList<T>.get_Item(Int32 index) at Artem.VisualStudio.Outlining.OutliningTagger.CalculateChange(TextContentChangedEventArgs e) at Artem.VisualStudio.Outlining.OutliningTagger.HandleBufferChanged(Object sender, TextContentChangedEventArgs e) at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.RaiseEvent[TArgs](Object sender, EventHandler`1 eventHandlers, TArgs args)

comments

Technolingua wrote Oct 18, 2012 at 6:06 AM

This has been bugging me for a while and I finally tracked it down to here. I'm not familiar with coding extensions, or how to install it to see if my code changes fix it, but this code looks like it lacks an index check:
Artem.VisualStudio.Outlining.OutliningTagger.CalculateChange():
    /// <summary>
    /// Calculates the change.
    /// </summary>
    /// <param name="e">The <see cref="Microsoft.VisualStudio.Text.TextContentChangedEventArgs"/> instance containing the event data.</param>
    protected virtual void CalculateChange(TextContentChangedEventArgs e) {

        // full refactoring is in progress, then no need to calculate
        if (this.FullRefactoring) return;

        // changes are more than 1, then set full refactor state
        if (e.Changes.Count > 1) {
            this.FullRefactoring = true;
            return;
        }

        ITextChange textChange = e.Changes[0];
        ....
The e.Changes needs a check such as if (e.Changes.Count == 0){ return; }
Or something similar so that it doesn't get to the e.Changes[0] if there isn't anything in that array.