1

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?

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98

3 Answers3

5

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.
Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
2

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

robowahoo
  • 1,259
  • 9
  • 10
  • This was interesting. I have a trace, but from start-to-end every step in the trace is in one internal MS library or another. Not sure where to go from here. Regardless, I guess your answer gets the big green checkmark. Thanks! – Grant Birchmeier Oct 18 '11 at 21:09
1

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

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • I altered the main method to add the try/catch, but it still didn't get caught. (I put a log statement in there so I'm 100% sure my additions didn't get clobbered by the code generator.) Thanks though, your link is informative. – Grant Birchmeier Oct 18 '11 at 20:00
  • In that case, the exception is almost certainly being thrown by a background thread. Are you doing any threading? – Chris Shain Oct 18 '11 at 20:08
  • Not really. I mean, I am doing WPF, so there's a certain amount of UI threading that happens implicitly, but I'm not doing anything fancy (yet). – Grant Birchmeier Oct 18 '11 at 20:13
  • Seems very suspicious, especially considering that the error doesn't happen in the debugger. Not to second guess you, but are you quite sure that you are running the same binary when in the debugger as when you are not? Try doing a Build->Clean and then running it from outside the IDE. If it still works, then you are not running from the same place. Also, check your build configuration- are you maybe debugging the Debug version, but running the Release version outside of the IDE? – Chris Shain Oct 18 '11 at 20:23
  • I was definitely running it from bin/Debug on the command-line. Also, I did a re-git-cloned my repo and built both Debug and Release from the command-line without opening the IDE - both binaries crashed the same. I'm not convinced it's not happening in the debugger, just that I'm not seeing it for some reason. – Grant Birchmeier Oct 18 '11 at 21:17
  • And now I reopened the IDE on that new clone and rebuilt, and the problem has magically went away. Frankly, I have no idea what black magic is going on here. I'm glad this isn't an app I'll be sellin'. – Grant Birchmeier Oct 18 '11 at 21:21