Archived

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

debug: Watch every change to a variable?

This topic is 5307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

One object in my code has a member variable whose value is not being set correctly. It causes a crash. Of course I can see its current value when that happens. But I''d like to be able to find what values it''s been assigned to along with the values of the other data members of this object. Is there any way the debugger can watch every variable and then let me get this information later (like when it crashes)? I could just log the changes to these variables and I''ll probably have to do that (but there are many of these objects so I would end up with a really enormous file I''d have to wade through...) but I''m wondering if there''s a way the debugger can do it. ~CGameProgrammer( );

Share this post


Link to post
Share on other sites
You can usually set the debugger to watch a variable and break when the value changes. What specific debugger are you using? Someone could probably help you more if we knew that information.

Share this post


Link to post
Share on other sites
The variable in question is part of a dynamically allocated object, and there are many instances of said object. Also the variable changes many times, correctly, before the crash. In other words this program, a 3D engine, works fine up until a point, then the crash. The debugger is just the built-in one for Visual C++ 6.0 Standard as I said [EDIT: Oops, no I didn't. I thought I did.]. Should I be using a third-party debugger?

By the way, I solved this particular error but I'd still like to know how to debug things like this. The problem was that a >= b - c should always be true with no exceptions, yet at one point a was too small. So I added a quick couple of lines in the one area of the code where a changes, in which if that expression is false then a divide by zero is performed, which of course brought up the debugger. Then I was able to debug stuff and in maybe 10 minutes I figured out the error.

But a more elegant solution would be nice.

~CGameProgrammer( );



[edited by - CGameProgrammer on June 5, 2003 4:31:58 PM]

Share this post


Link to post
Share on other sites
In Visual Studio 6, you can set conditions on your breakpoints and only break when the condition is true. In your case, you could set the breakpoint at that point in your code you were talking about and set the condition to be: !(a >= b - c). The breakpoint will only be triggered when that condition is true.

The other thing you can do without the debugger is to sprinkle assert()''s everywhere in your code on conditions you know should always be true. When you build in debug mode and the assert fails, a popup will appear telling you an assertion failed and will ask you if you''d like to debug the process. In your release build, the assert will be compiled away.

Share this post


Link to post
Share on other sites
Thanks guys. I''ve heard of ASSERTs and know they''re used often, and obviously they worked on conditions (like ASSERT(ReturnCode)) but didn''t know how exactly they worked. Is ASSERT a macro for something? It reminds me of exceptions (another thing I don''t use).

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
yes ASSERT is a macro, defined in assert.h as a conditional function call to _assert, the popup-and-ask-for-debug function.

Exceptions are a means of catching errors, once they have happened and allow the coder correct the error. IMO Exceptions should not be used to freely, as they quickly lead to not thought-through code.

Share this post


Link to post
Share on other sites
Write yourself a debugger add in using the macro system in VS.NET.

Have it record debug info for everything at given intervals and dump to a file. You could also include line numbers, function names, arguments. Every bit of information that the debugger has available to it is available to you through the macro and add-in systems.



Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.

Share this post


Link to post
Share on other sites
Microsoft''s debugging tools for windows can do exactly what you want http://www.microsoft.com/whdc/ddk/debugging/default.mspx

It''s an awesome tool, you can even write simple scripts when a breakpoint condition is true and of course log values of variables and registers to a file or the output window.

Just add a breakpoint.. print the value and then continue the program execution. I don''t remember exactly the command for that, but if you look at the help I''m sure you get it.

Share this post


Link to post
Share on other sites
The secret code, as I recall, is:

CTRL-B

It is well-hidden. There is no way to open the breakpoint window other than hitting ctrl-b . There is no menu option or toolbar button or anyhting for it. It will allow you to do what you want to do.

- Air

Share this post


Link to post
Share on other sites
quote:
Original post by Air
The secret code, as I recall, is:

CTRL-B

It is well-hidden. There is no way to open the breakpoint window other than hitting ctrl-b . There is no menu option or toolbar button or anyhting for it.


Besides Ctrl-B, you can:

This is for VC 6.0:
From the menu: Edit/Breakpoints....
Menu hotkeys: Alt-E, K
Keyboard shortcut: Alt-F9

This is for VC 7.0:
From the menu: Debug/New Breakpoint...
Menu hotkeys: Alt-D, B
Keyboard shortcut: Ctrl-B (as you mentioned)
Other method: Alt-F9 (or Ctrl-Alt-B or Debug/Windows/Breakpoints from the menu) and then click New in the breakpoints window, or right click in the breakpoints window and select New Breakpoint...

Not so well-hidden.

Share this post


Link to post
Share on other sites