本文介绍了Hashtable损坏(.Net bug?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序,其中多个线程将在同一个Hashtable上运行。

这个Hashtable通过使用Hashtable.Synchronized(myHashtable)

方法同步,所以没有在添加,删除或

迭代Hashtable之前,使用了更多的Lock语句。该程序在高工作负载环境中运行。

运行几天后,现在它突然捕获此异常时

插入一对密钥和对象,


stacktrace = System.NullReferenceException:对象引用未设置为对象的

实例。

at Mas.ShortDataTransportService.SdtsMsgKey.Equals( System.Collections.Hashtable.KeyEquals(Object item,Object key)
at System.Collections.Hashtable.Insert(Object key,Object nvalue, Boolean

add)

在System.Collections.Hashtable.Add(对象键,对象值)

在System.Collections.SyncHashtable.Add (对象键,对象值)



Mas.ShortDataTransportService.SdtsManager.InsertIn toPendingAckBuffer(SdtsOutMsgData dataItem)

at Mas.ShortDataTransportService.SdtsManager .HandleSe ndBufferMsg(Int32&

numSent)
Mas.ShortDataTransportService.SdtsManager.Transm的
它()


Mas下的类是我们自己创建的类。确保

插入的键和对象是非空值,否则ArgumentNullException

而不是NullReferenceException。将被抓住。

似乎Hashtable中的一些关键字已经变为Null,但是怎么能发生这种情况呢?因为Hashtable中的一个键永远不能为空,对吧?将

这是一个.Net Bug?

解决方案




开始时这是一个问题 - 迭代需要锁定

迭代的持续时间,否则在迭代期间可能会插入一个新值

,这会破坏事情。


来自文档for Hashtable.Synchronized:


< quote>

通过集合枚举本质上不是线程安全的

过程。即使集合同步,其他线程也可以修改集合,这会导致枚举器抛出

异常。为了在枚举期间保证线程安全,您可以在整个枚举期间锁定集合,或者捕获由其他线程所做的更改导致的

异常。

< / quote>


-

Jon Skeet - < sk *** @ pobox.com>


如果回复小组,请不要给我发邮件





Hashtable上运行。


Boolean


Mas.ShortDataTransportService.SdtsManager.InsertIn toPendingAckBuffer (SdtsOut
MsgData dataItem)在Mas.Short的Mas.ShortDataTransportService.SdtsManager.HandleSe ndBufferMsg(Int32&
numSent)中的


ArgumentNullException





I have a C# Program where multiple threads will operate on a same Hashtable.
This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable)
method, so no further Lock statements are used before adding, removing or
iterating the Hashtable. The program runs in a high workload environment.
After running a few days, now it suddenly catchs this Exception when
inserting a pair of key and object,

stacktrace = System.NullReferenceException: Object reference not set to an
instance of an object.
at Mas.ShortDataTransportService.SdtsMsgKey.Equals(Ob ject obj)
at System.Collections.Hashtable.KeyEquals(Object item, Object key)
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean
add)
at System.Collections.Hashtable.Add(Object key, Object value)
at System.Collections.SyncHashtable.Add(Object key, Object value)
at
Mas.ShortDataTransportService.SdtsManager.InsertIn toPendingAckBuffer(SdtsOutMsgData dataItem)
at Mas.ShortDataTransportService.SdtsManager.HandleSe ndBufferMsg(Int32&
numSent)
at Mas.ShortDataTransportService.SdtsManager.Transmit ()

Here classes under Mas are our self-created classes. It is sure that the
inserted key and object are non-null value, otherwise "ArgumentNullException"
instead of "NullReferenceException" will be caught.
It seems that some key inside the Hashtable has become Null, but how can
this happen? because a key inside a Hashtable can never be null, right? Will
this be a .Net Bug?

解决方案



That''s a problem to start with - iteration requires a lock for the
duration of the iteration, otherwise a new value could be inserted
during the iteration, which would break things.

From the docs for Hashtable.Synchronized:

<quote>
Enumerating through a collection is intrinsically not a thread-safe
procedure. Even when a collection is synchronized, other threads could
still modify the collection, which causes the enumerator to throw an
exception. To guarantee thread safety during enumeration, you can
either lock the collection during the entire enumeration or catch the
exceptions resulting from changes made by other threads.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





Hashtable.


Hashtable.Synchronized(myHashtable)


Boolean


Mas.ShortDataTransportService.SdtsManager.InsertIn toPendingAckBuffer(SdtsOut
MsgData dataItem)


"ArgumentNullException"


Will




这篇关于Hashtable损坏(.Net bug?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:10