AccessViolationException从System.Windows.Forms的,在使用WPF WinFormsHost [英] AccessViolationException from System.Windows.Forms, using WinFormsHost in WPF

查看:1170
本文介绍了AccessViolationException从System.Windows.Forms的,在使用WPF WinFormsHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WPF / WinForms的互操作应用程序时面临的一个讨厌的问题。我一直在试图解决这个问题了三天,但我无法取得任何进展。我怀疑我可以提供足够的信息以获得分辨率,但我正在寻找的人谁可以解释的这到底是怎么回事?

I'm facing a nasty issue when developing a WPF/WinForms interop application. I've been trying to resolve this issue for three days, but I'm unable to make any headway. I doubt I can provide enough information to get a resolution, but I'm looking for anyone who could explain what on earth is going on here?

我使用的组件是AxMapControl(ESRI的ArcGIS Engine 9.3.1 SP2),其中据我所知道的是COM包装的原生代码,公开为一个WinForms控制。该组件嵌入在我们的WPF(.NET 3.5)使用WPF WinFormsHost代理客户端软件。

The component I am using is AxMapControl (ESRI ArcGIS Engine 9.3.1 SP2), which as far as I know is COM-wrapped native code, exposed as a WinForms control. The component is embedded in our WPF (.NET 3.5) client software using WPF WinFormsHost proxy.

定期应用程序崩溃的硬盘与 AccessViolationException 。这总是发生在反应,在地图上控制用户的鼠标点击,但似乎没有要上哪些具体输入任何情由。堆栈跟踪总是相同的:

Periodically the application crashes hard with an AccessViolationException. This always happens in reaction to user's mouse click on the map control, but there doesn't seem to be any rhyme or reason on what specific input. Stack trace is always the same:

System.AccessViolationException:尝试读取或写入受保护的内存。这通常是指示其他内存已损坏。
在System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr的WNDPROC,IntPtr的的HWND,味精的Int32,IntPtr的的wParam,lParam的的IntPtr)
在System.Windows.Forms.NativeWindow.DefWndProc(消息和M)
。在System.Windows.Forms.Control.DefWndProc(消息和M)
在System.Windows.Forms.AxHost.WndProc(消息和M)
。在System.Windows.Forms.Control的。 ControlNativeWindow.OnMessage(消息和M)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和M)
在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,味精的Int32, IntPtr的WPARAM,LPARAM的IntPtr)

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.AxHost.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

由于异常,似乎被我的代码发起的任何调用堆栈之外被抛出,我想不通如何捕捉异常并以编程方式处理它。

Because the exception seems to be thrown outside any call stack initiated by my code, I can't figure out how to catch the exception and handle it programmatically.

这个问题happends在调试模式下,以及在发布版本。然而,它不会出现在所有计算机上,但我已经能够复制在Windows 7和XP这个问题,以及.NET Framework 3.5的和4.0。

This issue happends in debug mode, as well as in release builds. It does however not occur on all computers, but I have been able to replicate this issue on Windows 7 and XP, as well as .NET framework 3.5 and 4.0.

检查什么程序进行到处于崩溃的时候,异常似乎是,似乎有多个的CreateFileMapping 操作上GAC部署的DLL其失败的结果文件,其中仅有读者锁定

Inspecting what the process is up to at the time of a crash, the anomaly seems to be that there seem to be multiple CreateFileMapping operations on GAC-deployed DLLs which fail with the result FILE LOCKED WITH ONLY READERS.

这个观点已经被过滤,以显示该类型的结果只,但似乎这种情况正是两次每个DLL。这是否意味着什么?

This view has been filtered to show only results of that type, but it seems this happens exactly twice to each DLL. Does this mean something?

现在,很明显,我无能,发生了什么,以及如何解决这个问题。如果你有线索,你可能是善良,向我解释什么类型我处理问题的?

Now, it's obvious I am clueless as to what is happening, and how to resolve this issue. If you have a clue, could you be kind and explain to me what type of issue I am dealing with?

任何想法,我怎么能调试这个问题?

Any idea how I could debug this issue?

推荐答案

我使用的是比其他ESRI不同的地图控制权,但我的设置非常相似:包裹在COM本地地图代码,裹着Windows窗体控件,然后带入通过WindowsFormsHost WPF应用程序。 。而几个星期前,我得到了完全相同的System.AccessViolationException当我点击我的地图,除了极少数的选择来帮助调试

I'm using a different map control other than ESRI, but my setup is very similar: native map code wrapped in COM, wrapped in a Windows Forms control, then brought into a WPF application via WindowsFormsHost. And a few weeks ago, I got the exact same System.AccessViolationException when I clicked on my map, with very few options to help debug.

我的问题的罪魁祸首:在WPF底层控制的初始化不会发生我想它的方式 - WPF推迟某些初始化直到视图完全需要/在屏幕上看到(我假设,以减少窗口/控制加载时间)。在WPF中,我把我所有的初始化代码基础映射控制到一个构造函数和一个(WPF)UserControl.Loaded事件处理程序。问题是,WPF调用构造并引发Loaded事件之前的事情都是在屏幕上真正看到。所以我的基本地图控件正在用0高度,0宽度,这是合法的,但不正确的表面尺寸初始化。当我点击上图,我正在给底层地图控件的调用我的鼠标点击(X,Y)转换为纬度长,而且提高了AccessViolationException。

The culprit for my problem: initialization of the underlying control in WPF does not happen the way I thought it does -- WPF defers certain initialization until the view is fully needed/visible on the screen (I assume to reduce window/control load times). In WPF, I was putting all of my initialization code for the underlying map control into a constructor and a (WPF) UserControl.Loaded event handler. Problem was, WPF calls the constructor and raises the Loaded event before things are truly visible on the screen. So my underlying map control was being initialized with a surface size of 0 height, 0 width, which is legal but not correct. When I clicked on the map, I was making a call to the underlying map control to translate my mouse click (x, y) into a lat long and it raised the AccessViolationException.

我修复是重新初始化每当UserControl.Resize事件引发一种新的表面尺寸,这似乎发生可靠权的基础地图控件之前地图上被完全绘制在其合适的大小,并保持在直到地图被初始化为非零表面大小保持假我的WPF控制一个bool mapIsInitialized字段,适当的突起设置,并且在地图上被完全绘在第一次。而我的功能(例如,转换屏幕点经纬度LONS)访问基础地图控件现在什么也不做,除非mapIsInitialized。

My fix was to reinitialize the underlying map control with a new surface size whenever a UserControl.Resize event is raised, which seems to reliably happen right before the map is fully drawn at its proper size, and to keep a bool mapIsInitialized field in my WPF control that stays false until the map is initialized to a non-zero surface size, proper projections are set up, and the map is fully painted for the first time. And my functions (e.g. converting screen points to lat lons) accessing the underlying map control now do nothing unless the mapIsInitialized.

所以,你可能会或可能不会有类似的问题,但我会尝试跟踪周围的初始化代码,看看那被传递到底层的地图控制的参数值,看看它们是否有意义,如果你希望初始化正在发生,以在适当的值适当的时候。

So, you may or may not have a similar problem, but I would try to trace around the initialization code and look at the values of the arguments that are being passed to the underlying map control to see if they make sense and if the initialization is happening as you expect, with the proper values at the proper time.

祝你好运!

这篇关于AccessViolationException从System.Windows.Forms的,在使用WPF WinFormsHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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