debugging

24
Debugging

Upload: glain

Post on 26-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Debugging. Debugging Review. Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime . – Ancient Proverb - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Debugging

Debugging

Page 2: Debugging

Debugging Review

• Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. – Ancient Proverb

• Help a student to fix a bug, and he or she is set until the next bug. Teach that student to use the debugging tools, and maybe we can all get some sleep. – IOE 373 Proverb

Page 3: Debugging

Debugging Hint 1: Read the Error Message

• Not all error messages are easy to understand, but some are.• I’m always amazed at the forum posts which say “I ran this

code and got an error.”• Um…What did the error say???• By now, you should know what “Object reference not set to an

instance of an object” means (if not, wait a couple of slides).• But many other error messages are pretty straightforward, or

should be to you by now.• It’s frustrating when your code doesn’t work, but let VB help

you to solve the problem by reading the message, and then remember debugging hint 2…

Page 4: Debugging

Debugging Hint 2: Don’t Hit the Stop Button!

• If you run your code and you get to a point where it stops on a line of code, read the message, and then do…

• Nothing! When VB stops on a line of code, it is in debugging mode.

• By stopping on that line of code, VB is giving you a big hint: THERE’S SOMETHING WRONG WITH THAT LINE OF CODE!

• If you stop debugging, you will lose access to LOTS of information that can help you to solve the problem.

Page 5: Debugging

• In this example, we have experienced the most common error: “Object reference not set to an instance of an object.”

• VB doesn’t tell us which reference is not set—we need to find that out.

Page 6: Debugging

Don’t Stop That Program!• This information is pretty easy to find—if we

don’t stop the program.• We are looking for a variable with a value of

“Nothing”. • We can use Quick Watch to find it. Recall that

Quick Watch is available on the right-click menu; right click on the variable that you want to check, and select Quick Watch.

Page 7: Debugging

• QuickWatch on s indicates that it is not Nothing: It contains an actual Student object.

Page 8: Debugging

• However, a QuickWatch on the AllStudents List reveals that it is Nothing: We never created the List object by calling New.

Page 9: Debugging

Fixing the Bug• NOW, we can stop debugging. We know the cause of

the error: We are trying to add items to a List object that doesn’t exist yet.

• To fix the problem, we need to create the List before using it.

• Assuming that we will want to use that List elsewhere in the form’s code, the best places to create it would be when the form is created. Either:– The Form Load event, or– In the constructor, if we have written a custom constructor

for this form.

Page 10: Debugging

Form Load• Here is the form’s code using Form Load to create

the List:

Page 11: Debugging

Constructor

• Here is the form’s code using a custom constructor to create the List:

Page 12: Debugging

No Error, No Action?• What if you run your program, and it seems to

freeze?• That is, you do not get an error message, but

the program hasn’t done what it was supposed to do, and it doesn’t respond to mouse clicks?

• It is probably stuck in an infinite loop.

Page 13: Debugging

Debugging an Infinite Loop• If this happens, the first thing to do is to

switch to Visual Studio and click on the “Break All” button. (Pause)

Page 14: Debugging

Break All• Break All forces the program to pause

execution; VB will highlight the line of code currently running.

• You can then step through the code to see what’s happening.• In this case, the value of i never changes, so it is never

greater than 100.• The Watch Window can help you to see what’s going on.

(Next slide)

Page 15: Debugging

• In this case, the program has added 23027 zeros to the ListBox, and i is still zero.

• Whenever you use Do loops, make sure that the exit condition will eventually be met.

• For loops generally do not have this problem.

Page 16: Debugging

Other possible causes of infinite loops• Aside from Do Loops, there are other ways to end up in an

infinite loop:– Recursive subs/functions: These are subs or functions that call

themselves. These can be very useful, but they must always contain a way out, so that eventually they stop calling themselves.

– Calling back and forth. If Sub A calls Sub B, and Sub B calls Sub A, you can easily get caught in an infinite loop.

• In both of these cases, or any case where the program seems to be stuck, hit the “Break All” button.

• If the Call Stack is gigantic, you probably have one of these problems.

Page 17: Debugging

Startup Errors• Some of the most annoying errors are runtime errors where

the program crashes without stopping on a line of code.• These are very hard to find, since VB gives you very few

clues.• In most cases, I have found these errors to be caused by

writing executable code outside of any procedure.• In particular, the problem frequenty comes from calling

constructors (New) outside of any sub or function.• If your program fails on startup, check your code for places

where you have any code besides declarations (Dim, Public, Private) outside of procedures.

• See the next slide for examples.

Page 18: Debugging

Executable Code in Subs

• Instead of this,

• Do this:

Page 19: Debugging

When all else fails• Every semester, four or five students have

something go very wrong in their programs.• They were just about finished, most things

were working, and they try to add one last feature. All of a sudden…

• Nothing works! • Frequently, what has happened is that

something has become corrupted in the project file.

Page 20: Debugging

What to do• The solution to this situation (we’ll call it plan

B) usually takes about 15 minutes, so• Don’t spend more than 15 minutes or so

trying to figure out what went wrong before resorting to plan B.

• Plan B involves creating a new VB project and copying your forms and code into this new project.

Page 21: Debugging

Plan B, Step by Step1. Save everything you have, preferably in two places.

Make sure that you know the path to the directory that contains your forms and code.

2. Open Visual Studio and create a new VB project. Give it an appropriate name (Assignment6_PlanB, for example).

3. In the Solution Explorer, delete Form1 from the project.4. Without doing anything else, save this new project in a

directory.5. Note the directory which now contains the new project

file (Assignment6_PlanB.vbproj).

Page 22: Debugging

Plan B, Step by Step, continued6. Copy all of the code (*.vb), resource (*.resx), and designer

(*.Designer.vb) files from your old project directory to your PlanB project directory. (Don’t copy the vbproj, sln, or suo files.)

7. If you closed your PlanB project, reopen it in Visual Studio. If you had a Form1 in your original project, you should exclude

8. From the Project menu, select “Add Existing Item”.9. Select the forms and classes that you copied in from your

original projects.10. Note that for forms, you only need to select the vb file; Visual

Studio knows to add the Designer.vb and resx files along.

Page 23: Debugging

Plan B, Step by Step, continued11. In the project properties, Application tab, select the StartUp

form that you want.12. Try it out. If it works, save this new project and work with it

from now on.13. If it doesn’t work, there may be a problem with one

individual form or class.14. Plan C is to individually add or remove forms and classes to

see which one is causing the problem.15. If you isolate the problem form/class, you may be able to

recreate it by starting with a new form or class in this new project, and then copying the controls and code from the matching form/class in the old project.

Page 24: Debugging

Plan B Summary

• Resorting to plan B is a pain, but• It usually works, and• Spending 15 minutes on plan B is better than

spending hours staring at a project that isn’t working.