5 Replies - 4896 Views - Last Post: 13 October 2013 - 10:15 AM

#1 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Visual Studio IDE Debugger doesn't trap exception...

Posted 08 October 2013 - 10:33 AM

Here's another fun little issue I've seen while working with .Net and Visual Studio, and I'd like to get some discussion and opinions on the matter... or even a potential fix.

So this is a problem I've seen for some time and kind of just keep overlooking it. Thing is there are other people in the office who are coming against it as well, and they don't have as much .Net experience so depending on the debugger is big for them.

What happens is that in VB.Net when debugging code where an exception occurs, often inside a WinForms message (like the Closed event), instead of the debugger capturing it and bringing you to the line where the exception occurred. Instead the method it occurred in just exits, the entire call stack is dumped, and the program returns to the usual WinForms process pump as if idle.

If this happens in say the 'FormClosed' event, well the stacks dumps, and the form never actually closes because the code that the .Net framework does to actually close the form never executes because in my event handler for the event I happen to access an array with a bad index. (duh the fix is to not use a bad index... but that's the thing, we're debugging, we're expecting the debugger to say "HEY, RIGHT HERE DUMB ASS")

This usually isn't a huge problem for me, when I see it I know what's happening, and I just follow it on my own line by line and capture the error myself and figure it out. But this is time consuming, and not something I can easily teach to the rest of the staff.



Now this has been happening for quite some time, but I never really recorded what happened for showing an example. Today though I wrote down the chain of events that occurs to cause this. Note, I've attempted recreating this in simpler code elsewhere and couldn't get it to recreate. But it recreates over and over in the code that I found it in... so yeah, I'm just documenting this for the sake of documenting it and discussion.

When a button on a Form is pressed, Close() is called on that form.

Closing event raises, some validation occurs to make sure that we should allow closing, success.

Closed event raises and is captured by a collection in a 'FormManager' class I have. This collection listens for various events and raises another event saying that one of its members raised the Closed event. It's called 'BubbledFormClosed' and hands out args for which form was closed.

Manager listens for this bubbled event.

Manager on receiving the message finds an associated delegate that is used as a callback for when the form is closed (when you ask the FormManager to open a specific form by a String key, you can supply a callback to be called when it closes). This callback sometimes may be wrapped by another delegate for various reasons.

Manager calls the callback, passes in an object with some data about the closing form, this object is called a 'FormClosedVector'.

In this particular example the callback is in another form (the form that requested this closing form to be opened). It pulls some values out of the vector and calls a method on itself.

This method it calls accesses a Microsoft.VisualBasic.Collection object (ugh, big project being converted from VB6, working on removing all of these), the collection is looped over from 0 to Length - 1. But because Collection is a 1-based indexed object, should have been 1 to Length. IndexOutOfRangeException occurs.

Debugger doesn't catch exception, instead the method stops right there and exits, and the entire call stack for 'Close()' is dropped. If stepping through line by line, the debugger returns to where 'Close()' was called, and continues on... but the form never actually Closed.





So, what do you guys think?

This post has been edited by lordofduct: 08 October 2013 - 10:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Visual Studio IDE Debugger doesn't trap exception...

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3460
  • View blog
  • Posts: 11,727
  • Joined: 12-December 12

Re: Visual Studio IDE Debugger doesn't trap exception...

Posted 08 October 2013 - 11:35 AM

I apologise in advance if you consider this a silly statement ;) but if possible I would have very little code in the Closed event and use Closing in preference. (I bet you've already considered this :) )
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9195
  • View blog
  • Posts: 34,516
  • Joined: 12-June 08

Re: Visual Studio IDE Debugger doesn't trap exception...

Posted 08 October 2013 - 11:41 AM

... and are you compiling against 'release' or 'debug'?
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Visual Studio IDE Debugger doesn't trap exception...

Posted 08 October 2013 - 02:41 PM

Closing vs Closed - problem would still occur, we still need to be able to trap these errors with the debugger. It's not the result (that the form doesn't close) that we want to avoid, it's the debugger so that the more green programmers in the office can easily debug the issue (find the line of code that caused this).

Note the Closed event isn't the only place I've seen this. It's just that's where it happened today.

Furthermore, the code in question. No, it happens on the Closed event, not the Closing event. It's a callback that is called when a form is Closed... there is actually another callback that can be called on Closing to cancel the Closing of the form (there are rules that can be set up for a form, and these callbacks are used for the hooks to enforce the rules).



modi - debug

This post has been edited by lordofduct: 08 October 2013 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Visual Studio IDE Debugger doesn't trap exception...

Posted 10 October 2013 - 06:42 PM

Can you supply a sample project that does this so we can all run on our own systems?
Was This Post Helpful? 0
  • +
  • -

#6 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Visual Studio IDE Debugger doesn't trap exception...

Posted 13 October 2013 - 10:15 AM

I'd love to, but as I said I can't recreate.

It just randomly occurs, and it's occurring in the one project we have. Project is massive and I'm not actually allowed to share it.


I'm still playing around with it, if I can get it to a recreatable state, I'll definitely share, even if getting it recreated I'll probably figure out the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1