代码之家  ›  专栏  ›  技术社区  ›  Sam

new PathGeometry()引发TypeInitializationException?

  •  0
  • Sam  · 技术社区  · 13 年前

    我有一个程序,它在一个背景线程中进行一些几何分析。

    通常情况下,这工作得很好,但令人惊讶的是,现在我在开发计算机上创建PathGeometry时遇到了一个异常:

    因此,对于简单的代码:

    PathGeometry geometry = new PathGeometry();
    

    我得到一个System.TypeInitializationException{“Der Typeninitialisierer fr\”System.Windows.Media.PathGeometry\“hat eine Ausnahme verursacht。”}
    堆栈跟踪是:

    bei System.Windows.Media.PathGeometry..ctor()
    bei Bsoft.ilka.AlkisLeser.AlkisDatei.GetPosition(XElement xe) in AlkisDatei.cs:Zeile 267.
    

    此异常包含InnerException System.ComponentModel.Win32异常{“Unltiges Fensterhandle”} InnerException的堆栈跟踪为:

    bei MS.Win32.UnsafeNativeMethods.GetWindowLongWndProc(HandleRef hWnd)
    bei MS.Win32.HwndSubclass.UnhookWindowProc(Boolean force)
    bei MS.Win32.HwndSubclass.Dispose()
    bei MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
    bei System.Windows.Threading.Dispatcher..ctor()
    bei System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
    bei System.Windows.DependencyObject..ctor()
    bei System.Windows.Media.PathFigureCollection.get_Empty()
    bei System.Windows.Media.PathGeometry..cctor()
    

    奇怪的是:可执行文件确实在其他计算机上工作。

    问题出在哪里?为什么PathGeometry构造函数试图访问某些窗口句柄?我该如何解决此问题?

    我使用的是.NET framework 4.0,代码同时抛出VS2010和VS2012。

    1 回复  |  直到 13 年前
        1
  •  0
  •   Hans Passant    13 年前

    当本机CreateWindowEx()api函数调用失败时,您将获得此堆栈跟踪。它失败的原因很少,只有一个。您的程序很可能发生句柄泄漏,消耗了10000个窗口句柄。此时,Windows拒绝让您创建更多。

    使用Taskmgr.exe的“进程”选项卡进行诊断。查看+选择列,勾选“用户对象”。也勾选句柄和GDI对象,以防万一。观察流程显示的值。USER对象的值不断攀升意味着厄运。查找泄漏原因可能有点困难。当然可以考虑一个内存分析器。任意注释掉代码块。如果你在Taskmgr.exe中没有看到泄漏,那么你的进程就会受到某种其他进程的干扰。任意地一个接一个地杀死他们,寻找制造麻烦的人。

    最后但同样重要的是,看到一个工作线程创建一个窗口会带来麻烦。我对PathGeometry的了解还不够,但它看起来肯定不是那种可以在工作者身上正确工作的类。

    推荐文章