ConditionalWeakTable.GetOrCreateValue()中的IndexOutOfRangeException [英] IndexOutOfRangeException in ConditionalWeakTable.GetOrCreateValue()

查看:87
本文介绍了ConditionalWeakTable.GetOrCreateValue()中的IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   

我使用ConditionalWeakTable作为弱事件处理程序实现的一部分。它运行正常,除了偶尔我从用户那里得到自动崩溃日志,表明对GetOrCreateValue()的调用已经崩溃。



错误和调用堆栈看起来像以下两个堆栈,我假设取决于是否存在现有条目或需要创建新条目:

 System.IndexOutOfRangeException : 指数数组的边界之外。 
at System.Runtime.CompilerServices.ConditionalWeakTable`2.CreateEntry(TKey key,TValue value)
at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key,CreateValueCallback createValueCallback)
at [my code]


 System.IndexOutOfRangeException:Index超出了数组的范围。 
at System.Runtime.CompilerServices.ConditionalWeakTable`2.FindEntry(TKey key)
at System.Runtime.CompilerServices.ConditionalWeakTable`2.TryGetValueWorker(TKey key,TValue& value)
at System System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key,CreateValueCallback createValueCallback)$。$ $ b at [my code]

(对GetOrCreateValue()的实际调用似乎已内联。)



这是我的代码声明ConditionalWeakTable:

 private static readonly ConditionalWeakTable< object,Dictionary< EventInfo,object>> s_sourceObjects = new ConditionalWeakTable< object,Dictionary< EventInfo,object>>(); 

这是我调用GetOrCreateValue()的代码:


< pre class ="prettyprint"> Dictionary< EventInfo,object>事件;
lock(s_sourceObjects)
events = s_sourceObjects.GetOrCreateValue(sourceObject);

这里是引用s_sourceObjects字段的唯一其他地方:

 bool成功; 
Dictionary< EventInfo,object>事件;
lock(s_sourceObjects)
success = s_sourceObjects.TryGetValue(sourceObject,out events);

(我添加了锁定语句,因为我怀疑有一个ConditionalWeakTable中的线程安全错误,尽管文档声称它是线程安全的。但是,即使使用锁定语句,错误仍然会发生,所以显然它不是线程问题。)



我的应用程序使用.NET 4.6.2。



我是否在某种程度上滥用或不理解ConditionalWeakTable?或者这是一个框架错误?如果它是一个框架错误,有什么方法可以解决它吗?
b
$
这是该类的完整源代码,如果有人想查看上下文:https ://gist.github.com/waltdestler/2a339bdd0d7d647501eb4690772e3b50



感谢您的帮助!

解决方案

嗨Walt Destler,


  IndexOutOfRangeException  表示您正尝试使用无效索引按索引访问集合项
。当索引低于集合的下限或大于或等于它包含的元素数时,索引无效。


我检查你提供的代码,我不知道找到可能导致异常的地方。


您可以调试代码并提供获得异常的地方吗?


最好的问候,


Wendy


   

I am using ConditionalWeakTable as part of a weak event handler implementation. It works fine, except occasionally I get automatic crash logs from my users indicating that a call to GetOrCreateValue() has crashed.

The error and call stack looks like one of the following two stacks, I assume depending on whether there's an existing entry or a new one needs to get created:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.CreateEntry(TKey key, TValue value)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key, CreateValueCallback createValueCallback)
   at [my code]

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.FindEntry(TKey key)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.TryGetValueWorker(TKey key, TValue& value)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.TryGetValue(TKey key, TValue& value)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey key, CreateValueCallback createValueCallback)
   at [my code]

(The actual call to GetOrCreateValue() appears to have been inlined.)

Here's my code that declares the ConditionalWeakTable:

private static readonly ConditionalWeakTable<object, Dictionary<EventInfo, object>> s_sourceObjects = new ConditionalWeakTable<object, Dictionary<EventInfo, object>>();

Here's my code that calls GetOrCreateValue():

Dictionary<EventInfo, object> events;
lock(s_sourceObjects)
    events = s_sourceObjects.GetOrCreateValue(sourceObject);

And here's the only other place that references the s_sourceObjects field:

bool success;
Dictionary<EventInfo, object> events;
lock(s_sourceObjects)
    success = s_sourceObjects.TryGetValue(sourceObject, out events);

(I added the lock statements because I suspected there was a thread-safety bug in ConditionalWeakTable despite the documentation claiming it's thread-safe. However, the bug still occurs even with the lock statements, so apparently it's not a threading issue.)

My application uses .NET 4.6.2.

Am I somehow misusing or not understanding ConditionalWeakTable? Or is this a framework bug? If it's a framework bug, is there any way I can work around it?

Here's the entire source code for the class, if anyone wants to see the context: https://gist.github.com/waltdestler/2a339bdd0d7d647501eb4690772e3b50

Thanks for your help!

解决方案

Hi Walt Destler,

The IndexOutOfRangeException means that you're trying to access a collection item by index, using an invalid index. An index is invalid when it's lower than the collection's lower bound or greater than or equal to the number of elements it contains.

I check the code you provided, I do not find the place which could cause the exception.

Could you debug your code and provide where you get the exception?

Best Regards,

Wendy


这篇关于ConditionalWeakTable.GetOrCreateValue()中的IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆