1

我不明白为什么我今天收到了System.IndexOutOfRangeException。当程序已经运行了 8 个小时并且发生异常的地方被执行了数百万次时,我已经收到了它。这个地方很简单:

for (int i = 0; i < _goldDesiredOrdersBuy.Length; i++)
{
    _goldDesiredOrdersBuy[i] = -1;               // IndexOutOfRangeException! Strategy3.cs:line 666
}

我仅在程序启动时初始化 _goldDesiredOrdersBuy 一次,因此可以保证在发生异常时对其进行初始化:

private int[] _goldDesiredOrdersBuy = new int[MaxOrderbookDepth];

我有另一个地方可以触摸这个数组:

    private int GetGoldVolumeBuy(int bidQuotesPos)
    {
        if (_goldDesiredOrdersBuy[bidQuotesPos] > -1)
        {
            return _goldDesiredOrdersBuy[bidQuotesPos];
        }
        int result = GetGoldVolumeBuyNotCached(bidQuotesPos);
        _goldDesiredOrdersBuy[bidQuotesPos] = result;
        return result;
    }

就这样。_goldDesiredOrdersBuy应用程序启动时初始化一次,保证被初始化并且数组的长度不会在任何地方修改,所以我不明白我是如何收到IndexOutOfRangeException的,有什么想法吗?

Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. --->
   System.AggregateException: One or more errors occurred. --->
   System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at MyProj.Strategies.Strategy3.CalculateNewDesiredOrdersBuy() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 666
   at MyProj.Strategies.Strategy3.RecalculateBuyOrders() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 567
   at MyProj.Strategies.Strategy3.OnAllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 499
   at MyProj.Strategies.Strategy3.AllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 413
   at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
   at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
   at System.Threading.Tasks.Task.<>c__DisplayClass7.<ExecuteSelfReplicating>b__6(Object )
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
   at MyProj.Market.FinishUpdatingTables() in C:\Oleg\projects\MyProj\MyProj\Market.cs:line 449
   at Library.Exchange.Gate.DoGateIteration() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 143
   at Library.Exchange.Gate.<Connect>b__0() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 98
   at System.Threading.Tasks.Task.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.TaskExceptionHolder.Finalize()
4

4 回答 4

2

来自System.ArrayMSDN

不保证任何实例成员都是线程安全的。

通过在多个线程之间共享此数组实例,您将违反文档。您应该正确锁定。

于 2012-07-02T18:21:16.307 回答
1

Imo的问题是您有几个函数可以接受类似int index(或类似的)参数并使用该参数从数组中恢复数据。

数组的大小没有改变,没关系。但是,即使您的数组长度是静态的并且等于(例如)5,如果您尝试使用 访问该数组index>=5,您将得到您描述的异常。

编辑

考虑到在循环内引发异常的另一种可能性for(...),其边界用数组本身的大小标记,我想说考虑到执行堆栈,你并行执行东西。因此,我认为此时数组的大小会以某种方式发生变化。

要检查这一点,您可以尝试执行以下操作:

int limit = _goldDesiredOrdersBuy.Length; //SET LIMIT BEFORE ITERATION
for (int i = 0; i < limit ; i++)
{
    _goldDesiredOrdersBuy[i] = -1;           
}

并检查错误是否仍然发生。

希望这可以帮助。

于 2012-07-02T17:58:13.217 回答
0

好吧,这个方法不检查传入的有效索引。
所以我将尝试在这里进行一些安全检查。

private int GetGoldVolumeBuy(int bidQuotesPos) 
{ 
    int result = -1;
    if(bidQuotesPos >= 0 && bidQuotesPos < _goldDesiredOrdersBuy.Length)
    {
        if (_goldDesiredOrdersBuy[bidQuotesPos] > -1) 
        { 
            return _goldDesiredOrdersBuy[bidQuotesPos]; 
        } 
        result = GetGoldVolumeBuyNotCached(bidQuotesPos); 
        _goldDesiredOrdersBuy[bidQuotesPos] = result; 
    }
    return result; 
} 

我假设这result=-1是一个有效的输出

于 2012-07-02T17:58:05.770 回答
0

您没有检查 的值是否bidQuotesPos在数组范围内。

private int GetGoldVolumeBuy(int bidQuotesPos) 
{ 
    if (bidQuotesPos < 0 || bidQuotesPos >= _goldDesiredOrdersBuy.Length)
    {
        // either throw an exception, or (less desireable) return a value so that the caller
        // knows that an error occurred.
        return HandleInvalidInputValue(bidQuotesPos);
    }

    if (_goldDesiredOrdersBuy[bidQuotesPos] > -1) 
    { 
        return _goldDesiredOrdersBuy[bidQuotesPos]; 
    } 
    int result = GetGoldVolumeBuyNotCached(bidQuotesPos); 
    _goldDesiredOrdersBuy[bidQuotesPos] = result; 
    return result; 
} 
于 2012-07-02T17:58:45.167 回答