C#/WPF app throwing a ObjectDisposedException; why

2019-06-08 06:02发布

I have a C# WPF UI app, and when I close it, I always get a windows application-crash dialog ("UIDemo has encountered a problem and needs to close.").

The error report indicates that it's a System.ObjectDisposedException which indicates that somewhere a method's being called on a disposed object. That's fine, I do understand that part.

And I would love to fix it. I just can't get a stacktrace on the bastard.

That exception is evading all of the following:

  • my DispatcherUnhandledException handler
  • my try/catch surrounding the entire contents of the Exit event handler
  • clicking "Debug" in that windows application-crash dialog closes it and doesn't do anything
  • running the app in VisualStudio's Debug mode seems like it would work, but, strangely, it doesn't seem to crash this way, so no trace

All I have to go on is the arcane contents of the error report that Windows to send to MS. These hexidecimal dumps of memory aren't really that useful.

Does anyone know how I can get that darn trace?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-08 06:27

A console window will show any exceptions thrown with a full stack trace. To add a console window to your WPF application:

  1. Go the project properties of your WPF application.
  2. Select the first (side-) tab, Application.
  3. Under Output type select Console Application.
  4. Build and run your app. It should start up with an additional console window. When it crashes you should see the exception and stack trace in it.
查看更多
你好瞎i
3楼-- · 2019-06-08 06:29

Try putting a try/catch inside of your main method, not around the exit handler. In WPF, the main method isn't always easy to find- see here for how to find it: http://joyfulwpf.blogspot.com/2009/05/where-is-main-method-in-my-wpf.html

查看更多
smile是对你的礼貌
4楼-- · 2019-06-08 06:31

Another option would be to DebugDiag to catch that particular exception and generate a crash dump which could be analyzed in WinDbg via psscor2 or SOS. That will allow you to evaluate the stack trace.

DebugDiag: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=24370

Debugging CLR with WinDbg: http://www.codeproject.com/KB/debug/windbg_part1.aspx

查看更多
登录 后发表回答