English 中文(简体)
字典<TKey,资料来源>神秘行为
原标题:Mysterious behavior of Dictionary<TKey, TSource>

我正在研究一个以Asp.net MVC 3.0为基础的大型系统,并研究莫诺-2.10.8(窗口7)。

一切都很好 直到几天前

我的APIII内有几门使用字典的实用课程,例如:

public static class KeyUtility  
{
  static KeyUtility() {
    Alphabet = new[] {
       A ,  B ,  C ,  D ,  E ,  F ,  G ,  H , 
       J ,  K ,  L ,  M ,  N ,  P ,  R ,  S , 
       T ,  U ,  V ,  X ,  Y ,  Z ,  0 ,  1 , 
       2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 
    };

    ReverseAlphabet = Alphabet
      .Select((c, i) => new { Char = c, Value = i })
      .ToDictionary(k => k.Char, v => (byte) v.Value);
  }

  internal static char[] Alphabet;      
  private static IDictionary<char, byte> ReverseAlphabet;

  public static string ToKey(byte[] key, int groupSize)
  {
    //Accessing Alphabet to generate string key from bytes
  }

  public static byte[] FromKey(string key)
  {
    //Accessing ReverseAlphabet to get bytes from string key
  }
}

我随机得到这样的例外:

System.IndexOutOfRangeException: Array index is out of range.
at System.Collections.Generic.Dictionary`2<char, byte>.TryGetValue (char,byte&) <0x000a1>
at MyAPI.KeyUtility.FromKey (string) <0x0009a>
at MyApp.Controllers.AboutController.Index () <0x002df>
at (wrapper dynamic-method) object.lambda_method (System.Runtime.CompilerServices.Closure,System.Web.Mvc.ControllerBase,object[]) <0x0002f>
at System.Web.Mvc.ActionMethodDispatcher.Execute (System.Web.Mvc.ControllerBase,object[]) <0x0001b>
at System.Web.Mvc.ReflectedActionDescriptor.Execute (System.Web.Mvc.ControllerContext,System.Collections.Generic.IDictionary`2<string, object>) <0x000ff>
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod (System.Web.Mvc.ControllerContext,System.Web.Mvc.ActionDescriptor,System.Collections.Generic.IDictionary`2<string, object>) <0x00019>
at System.Web.Mvc.ControllerActionInvoker/<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12 () <0x00066>
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter (System.Web.Mvc.IActionFilter,System.Web.Mvc.ActionExecutingContext,System.Func`1<System.Web.Mvc.ActionExecutedContext>) <0x000b8>

Most of the time everything is fine and KeyUtility works correct, but on rare occasions I get such an exception. Despite it looks like thread safety issue, the dictionary ReverseAlphabet is always accessed for reading only and never for writing. Once it s created in static constructor it s only accessed with TryGetValue. As I understand from MSDN article it should be thread safe in this case. Moreover, I haven t seen this problem before.

我甚至不能创造复制品,因为我完全不知道什么是错的。

单一至2.10.8和旧版本在字典中被证明是稳定的。 几年来我一直在大量使用它,而且从未见过这种例外。

如何解决这个问题?

<强>UPD:

我记得,在开始出现麻烦的时候,我所做过的事情与我的可执行文件(i m 嵌入一个单词到我的应用程序中) 静态地连接单词。 我简单地下载了单词源。 不加任何修改地将它连接起来, 除了我设置了 libmono s 输出到静态库。 我还与 libeay32 和 sqlite3 连接了。 所有的多线( MT) 。 也许这个变化会影响一个应用程序? 不幸的是, 我无法在独立单词下检查这个程序 。 在此之前, 我将所有图书馆动态地连接起来, 一切都很好 。

UPD2: Here is the link to the complete sources: http://pastebin.com/RU4RNCki

最佳回答

我所做的一切 都和我的可执行文件 静态地连在一起

您已经知道我所想的问题, 这肯定会打破单项。 当你静态地连接它时不再发生的最重要的事情是 Windows 所做的 DllMain () 回调, 当新线条在进程开始执行时, 它会做出这样的回调。 这就是 CLR 是如何意识到线条, 即 may 执行管理代码的。 静态构建器的确是一个可能的失败模式, 在 Cctor () 完成执行之前, 线条必须被屏蔽, 无法在类内执行任何代码 。 如果 CLR 不了解线条, 则 CLR 无法这样做 。

如果您想要完成这项工作, 您至少需要为您自己的 DllMain () 函数调用 $ 1_ tread_ info_ attach () 提供替代选项。 一般来说, 我认为这是一次优化太多 。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...