[翻译]  Explain critical bug in Visual Studio 2010 and up, WinForms and WPF

[CHINESE]  解释Visual Studio 2010及更高版本,WinForms和WPF中的关键错误


Try putting the following code inside Load event handler for WinForms or Loaded for WPF.

尝试将以下代码放在WinForms的Load事件处理程序或WPF的Loaded中。

Dim doc As New XmlDocument
Dim nsmgr As New XmlNamespaceManager(Nothing) 'this line throws an exception

Problem is that exception is not thrown, and stack corruption happens. It may have different side effects, depending on the IDE - see below.

问题是不抛出异常,并发生堆栈损坏。它可能有不同的副作用,具体取决于IDE - 见下文。

  • Affected IDEs are: 2008, 2010 and 2012 (those I could test). 2010 resets stack state, and returns from sub/handler, like nothing happened (but without proceeding with other statements there). 2012 may warn a user about a failed application and an attempt to run in compatibility mode. Next time after that it runs the same as 2010. 2008 properly throws an exception, but only on default configuration (AnyCPU). Switching platform target to x86 makes the problem reappear in 2008 as well.
  • 受影响的IDE是:2008年,2010年和2012年(我可以测试的)。 2010重置堆栈状态,并从子/处理程序返回,就像没有发生任何事情(但没有继续其他语句)。 2012可能会警告用户应用程序失败并尝试以兼容模式运行。之后的下一次运行与2010年相同。2008年正确抛出异常,但仅限于默认配置(AnyCPU)。将平台目标切换到x86也会使问题再次出现在2008年。
  • Affected frameworks are WinForms and WPF. Console apps and ASP.NET seem to work fine. .NET v2.0-4.5.
  • 受影响的框架是WinForms和WPF。控制台应用程序和ASP.NET似乎工作正常。 .NET v2.0-4.5。
  • Affected scope is only Load event so far. Putting this code into a button makes it work.
  • 受影响的范围目前仅为Load事件。将此代码放入按钮使其工作。
  • Affected build configuration = any. Tried on default Debug and Release.
  • 受影响的构建配置= any。试过默认的Debug和Release。

Why I consider it a bug is because it can leave objects in an unstable state - they did not finish initializing, which is not an expected behavior. What's critical about it is that nobody will know it happened, as it does not throw an exception. Depending on your design, you may end up with incorrect data in your database, which in the worst case may lead to severe consequences.

为什么我认为它是一个错误是因为它可以使对象处于不稳定状态 - 它们没有完成初始化,这不是预期的行为。重要的是,没有人会知道它发生了,因为它没有抛出异常。根据您的设计,您最终可能会在数据库中显示不正确的数据,这在最坏的情况下可能会导致严重的后果。

Does anyone have a good explanation to why this may be happening and if there is a workaround?

有没有人能够解释为什么会发生这种情况以及是否有解决方法?

1 个解决方案

#1


2  

The problem is caused by the wow64 emulation layer that comes into play when you target x86 platform on a x64 OS.
It swallows exceptions in the code that is responsible to fire the Load event.
Thus the debugger doesn't see the exception and cannot step in to handle the situation.
This article seems to document well what's happening there,

问题是由于在x64操作系统上定位x86平台时发挥作用的wow64仿真层引起的。它吞噬了负责触发Load事件的代码中的异常。因此,调试器不会看到异常,也无法介入以处理该情况。这篇文章似乎很好地记录了那里发生的事情,

This previous answer from Hans Passant (to which goes all the credits and upvotes) explains possible workarounds.
My preferite one is to move everything out of Form_Load event and put the problematic code in the form constructor. (Of course I don't know if it is applicable in your case)

Hans Passant之前的回答(所有学分和赞成)解释了可能的解决方法。我最喜欢的是将所有内容移出Form_Load事件,并将有问题的代码放在表单构造函数中。 (当然我不知道它是否适用于你的情况)


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号