Archived

This topic is now archived and is closed to further replies.

Talib

Effictive coding and debugging help

Recommended Posts

Hi I am working with VC++ 6.0 and I have never used the debugger (or any debuuger on any compiler I have ever worked with). I recently also started with directx and am feeling like I am missing out on an important tool, especially in finding memory leaks. Can anyone help me explore this tool. (for some reason the search on this forum bombs out when I use the word debug). Thanks Talib

Share this post


Link to post
Share on other sites
The debug tool in vc++ does not much more than allowing you to query variable values and whatnot while the program is running. It won''t find you memory leaks, you''ll have to set that up yourself with a few debug tags.

Just have a play around with it yourself, it''s much easier to actually see what it does rather than trying to understand someone''s explanation of what it does.

Share this post


Link to post
Share on other sites
Definitely. mastering a good debugger, like in VC6, is quite essential.

where to start... there so MANY things you can do, and I don't know any tutorials.

I'll give an introduction anyway.

1) project settings
-------------------
in "Project"->"project settings".
"C/C++"->"General" : the most useful debug info is the "Prograsm Database for Edit and Continue". I'll explain later
"Link"->"Generate Debug Info" on.

as well, in general debug builds, generating browse info is very useful.

"Edit and continue" allows you to change portions of the code, while in a debug session, and the modifications will be re-compiled, but instead of restarting the debug session from zero, the program execution will continue as before, with the new altered code. It's very cool, when it works! It's not quite 100% bug free Edit and continue will not work if you do things like add a new function, or add or remove a global/const variable, change its value (there are other ways for that), or generally, change a header (too many files had to be recompiled.

2) debugging session
--------------------
- Adding breakpoint is easy. Make sure the code is build before-hand, or the breakpoints might not be put where you expect them. F9/Crtl-F9 to toggle a breakpoint, or disable it momentarly. The program execution will halt when hitting the breakpoint.

- Running a debug session : Press F5 to start/continue a debug session, that will stop on the first breakpoint. Ctrl-10 to run the program until it hits the line of code under the cursor, F10 to step through the code one line at a time. F11 allows you to step into the portion of the code at the current line.

- playing with breakpoints : so, F9/Ctrl-F9, and press Ctrl-B to edit the breakpoints. To add conditions, hit the "condition", and enter the condition where the code would stop at that specific breakpoint ("Enter The expression to be evaluated :"->"(i == 2) || (MyData.m_iIndex == -1)).

you can also add data breakpoints. You specify an address or a variable, and the program will halt when the content of that address is changed. very useful if you have some memory stamping problems, to see what's writing on what part of the memory. Or also when a variable changes value, ect....

so, you have breakpoints, and you can step through the program... not very useful.

in the VC6 toolbar, press the second button, and select the "watch" and "variable" windows. In there, you can see some variables of the code changing values as you step through the program or hit a breakpoint. you can add variables as well (drag and drop a variable from the source window into the variable window, on the right, you'll see it's value). when the program execution is halted (breakpoint of step by step with F10/F11), you can change values of a variable in the variable/watch window by double clicking on its value. You can remove variables from the variable window by highlighting the variable and pressing 'delete'. You have several tabs of variable windows, so you can organise them better.

when debugging an array, say you want to see the values in the variable int MyArray[10], in the variable window, add the watch variable "MyArray,10", with no space, and you can expand the array and see the values in the first 10 elements of the array. or you can do (&MyArray[5]),5, to see the last 5 elements.

in the watch window, it very similar to the variable window. you also have a tab at the top, called the "call stack" (called the "Context"). from there, you can trace the program back in time.

in the variable window and watch variable, only variables available on the stack (function parameters, variable created on the stack and available for the 'context'), and global variables. you can watch variables like MyRenderer::RenderCounts, if RenderCounts is a global variable of the class MyRenderer, for example.

another thing. VC6 also uses a sort of script files, to quickly view values of a structure type, without expanding the structure to see what's inside it.

in "Program Files\Microsoft Visual Studio\Common\MSDev98\Bin\AUTOEXP.DAT" (auto-export.dat, I think), you can specifiy format of structures to watch.
say for example you have a structure/class

struct Vector { float x, y, z; }.

in autoexp.dat, add the line


Vector=<x,f>, <y,f>, <z,f>



...all that should get you going

[edited by - oliii on May 27, 2004 4:38:51 AM]

Share this post


Link to post
Share on other sites
as for memory leaks, you can use third party libraries, like memwatch. they override new/delete, and help track down memory leaks/stamping. There are problems with using them with STL, and memwatch.h should be put at the top of the header includes in your files. But very useful noneless.

Share this post


Link to post
Share on other sites
The best thing is just practice stepping through your code with the debugger to get a feel for it. I started out not using it, but as soon as I tried it I never looked back. I use it to solve problems in 5 minutes that would take hours/days/weeks if I didn''t have it. It''s fantastic for tracking variables and finding where things went awry. Note, you''ll want your game to run in windowed mode unless your video card supports multiple monitors.

Share this post


Link to post
Share on other sites