多线程应用程序崩溃 - 可能是内存损坏?
我有一个 C# 多线程应用程序,它基本上使用 lock() 来访问字典。有 2 个线程,一个消费者和一个生产者。锁定机构非常简单。该应用程序在重负载下运行数周没有出现问题。
今天它刚刚崩溃了。我深入研究 WinDbg 来查看异常,它是访问字典时的 KeyNotFound。
哪些问题可能导致这次崩溃?我是否应该考虑内存损坏最终是否可能发生?
I have a multi-threaded application in c# which basically uses lock() to access a dictionary. There are 2 threads, a consumer and a producer. The locking mecanism is very simple. This application runs under heavy load for weeks without a problem.
Today it just crashed. I digged into WinDbg to see the Exception and it was a KeyNotFound when accessing the Dictionary.
What problems could cause this crash? Should I consider that memory corruption eventually may occur or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使锁定太细粒度可能会导致这种情况。例如:
然后像这样使用它:
那是行不通的,字典可能会在 hasKey 和 getValue 调用之间发生变化。整个操作需要锁定。是的,大约每个月就会出错一次。
Making the locking too fined-grained could cause this. For example:
And then using it like this:
That won't work, the dictionary could change between the hasKey and the getValue call. The entire operation needs to be locked. And yes, this goes wrong once a month or so.
我们在使用dictionary.ContainsKey时注意到CMS框架代码中存在各种问题
...相反,我们更改了代码以使用try..catch块,令人惊讶的是,它解决了我们的问题...也请尝试将您的函数更改为像这样的事情:
看看是否有帮助......
We have noticed various problems in our CMS framework code while using
dictionary.ContainsKey... instead we changed the code to use a try..catch block and amazingly, it has fixed our issues... please also try to change your function to something like this:
see if it helps...