【问题标题】:How to understand at which line/method to check for below C# exception?如何理解在哪一行/方法检查以下 C# 异常?
【发布时间】:2018-03-10 10:55:34
【问题描述】:

我是这个领域的新手,所以不知道如何修复崩溃/异常。我见过带有类似错误消息的问题,但也有一些代码;因此,这些人清楚地知道在哪里调查。但是这里我只有一个堆栈跟踪,不知道从哪个方法开始。

Collection was modified; enumeration operation may not execute.:Source:mscorlib:StackTrace:  at System.Collections.Generic.Dictionary`2+Enumerator[TKey,TValue].MoveNext () [0x00016] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Net.Http.Headers.HttpHeaders+<GetEnumerator>d__19.MoveNext () [0x000ac] in <609d052db9cf4644bbd3271148e63c5f>:0 
  at System.Net.Http.Headers.HttpRequestHeaders.AddHeaders (System.Net.Http.Headers.HttpRequestHeaders headers) [0x00025] in <609d052db9cf4644bbd3271148e63c5f>:0 
  at System.Net.Http.HttpClient.SendAsync (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x000be] in <609d052db9cf4644bbd3271148e63c5f>:0 
  at System.Net.Http.HttpClient.SendAsync (System.Net.Http.HttpRequestMessage request) [0x00008] in <609d052db9cf4644bbd3271148e63c5f>:0 
  at System.Net.Http.HttpClient.GetAsync (System.Uri requestUri) [0x0000c] in <609d052db9cf4644bbd3271148e63c5f>:0 
  at Tom.Arsene.Data.RestClient+<GetAsync>d__11`1[T].MoveNext () [0x00090] in <1f81a31c6d47431bab8f6adc642df7b4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at Tom.Arsene.Data.ArseneServiceAgent+<GetArsene>d__2.MoveNext () [0x000c3] in <1f81a31c6d47431bab8f6adc642df7b4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at Tom.Arsene.Business.ArseneService+<GetArsene>d__15.MoveNext () [0x00145] in <1f81a31c6d47431bab8f6adc642df7b4>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <1bdfc850d6954268ba9a73eeb78e5ccf>:0 
  at Tom.Droid.PushHandlerService+<>c__DisplayClass20_0+<<ProcessActiveSilentNotification>b__0>d.MoveNext () [0x00078] in <b12c833322824aa0abff3963b0b59352>:0

当我进行初步研究时,当我们尝试在 foreach 循环中修改正在枚举它的集合时,会导致此异常。但是在 ProcessActiveSilentNotification 中没有 foreach 循环似乎是由此产生的。

如果这听起来很烦人,我很抱歉,但请帮帮我。对您来说,这可能看起来只是另一个重复的问题,但解决方案/提示可以让我睡个好觉。谢谢

【问题讨论】:

  • HttpRequestHeaders.AddHeaders 通过 Tom.Arsene.Data.RestClient
  • 感谢您的回复!您能否让我知道您是如何确定这种方法导致问题的?此外,AddHeaders 不是解决方案中的书面代码,而是在库中。所以我想我们不能在那里修复..
  • 在迭代集合时,不要修改集合的索引。

标签: c# android exception xamarin stack-trace


【解决方案1】:

啊这个。

背景:您的核心是正确的:foreach and some other code 不适用于集合。它仅适用于枚举器。虽然任何集合都可以转换为枚举器并且是隐式的,但枚举器具有比普通旧集合更严格的规则。从 Collections 创建的 Enumerators 的核心规则之一是:如果基础集合发生更改,则任何 Enumerator using it must invalidate。此规则与 Dispose 和 Finalize 一样重要。

最可能的原因:您正在尝试修改集合,而其他代码(您编写或使用的代码)正在通过枚举器处理它。

它可能是一直存在的代码,但由于缺少输出而被 JiT 简单地删除,因此被视为“死代码”。就像在这个例子中试图在 x32 系统上遇到 2 GiB 限制一样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOM_32_forced
{
    class Program
    {
        static void Main(string[] args)
        {
            //each short is 2 byte big, Int32.MaxValue is 2^31.
            //So this will require a bit above 2^32 byte, or 2 GiB
            short[] Array = new short[Int32.MaxValue];

            /*need to actually access that array
            Otherwise JIT compiler and optimisations will just skip
            the array definition and creation */
            foreach (short value in Array)
                Console.WriteLine(value);
        }
    }
}

如果您删除输出,JiT 很有可能会删除数组定义和创建,从而减少应用程序的实际使用。从而有效地防止了我在这里试图强制的异常。

避免遇到此类风险的一种简单方法是克隆。您只需创建一个单独的集合并将其交给您那里的任何代码。如果需要浅拷贝或需要深度克隆,则取决于您使用的类型。对于不可变类型(大多数构建结构/原始类型和字符串),浅拷贝可能就足够了。当然,你是否可以进行克隆取决于你在做什么。

如果您不向我们展示任何代码,我们只能告诉您这么多。

【讨论】:

    猜你喜欢
    • 2019-06-30
    • 2021-07-31
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2013-02-18
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多