Sign in to follow this  

Useful Programming Tip

This topic is 4759 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

Well, since I don't have a column here, I thought I might as well share my advice here. I'm currently working on a textmode raytracer. I was just converting my raytracer from a scanline renderer to a tile renderer to better utilize the cache. So I ran into this strange problem that happened during rendering. It would seem that all it rendered was a single offscreen tile. Unfortunately I found out that it was happening deep into the rendering cycle. This would mean that everytime I needed to see if it got fixed, I'd need to press the Step Over (F11) key about 800 times. I realized that there's no conditional breakpoint command in Visual Studio. Here's the important part. I suddenly remembered something from my Computer Organization (Pentium Assembly) course in University. The key is software interrups. So I hurridly coded up what I think will be very useful to everyone. So here it is.
void BreakIf(bool condition)
{
   if(condition)
   {
      __asm
      {
         int 3
      }
   }
}

What this code will do is insert a breakpoint on the line that says "int 3". It will move the processor into single-step mode, and thus Visual Studio will also move into single-step mode allowing you to use all of your F10, F11, etc... keys. To get to your code, just keep stepping until you get there. So what do you think? Useful? Yes? No? Should I write some more useful tips like this (I've got a few more)?

Share this post


Link to post
Share on other sites
Quote:
Original post by Reaper_Unreal
I realized that there's no conditional breakpoint command in Visual Studio.


Yes there is.

Right click on the line you have the breakpoint set on, go to breakpoint properties and there is a conditional button you can enter in a condition you want it to break on.

Also if you ever have an infinite loop somewhere and you dont know where you can force the program to break with CTRL-ALT-BREAK and that will force the program to break wherever the instruction pointer currently is.

Share this post


Link to post
Share on other sites
That's exactly the way I implemented my own assert macro :D


#ifdef _DEBUG
#define EAssert(x, msg) if(x) { EDebugOutput(msg);__asm int 3;}
#else
#define EAssert(x, msg)
#endif

Share this post


Link to post
Share on other sites
Visual Studio has many break point options infact. Conditional ones and even "Break on Write" style breakpoints (if not using managed code, beacause of managed memory dancing you can't do it or something)

Share this post


Link to post
Share on other sites
Quote:
Original post by Keem
Visual Studio has many break point options infact. Conditional ones and even "Break on Write" style breakpoints (if not using managed code, beacause of managed memory dancing you can't do it or something)

You can do it in an unmanaged block inside managed code if you want (using the unsafe specifier). But you can't break-on-write in managed code because the GC frequently moves lightweight objects around in blocks when it frees up larger memory chunks. So a breakpoint that's watching a specific location in managed memory would get falsely triggered if the GC moves the object.

Share this post


Link to post
Share on other sites
I usually just do something of the form

// Inside function or loop:
static int count = 0; count++;

if(count == 10000)
{
int q = 0; // Put breakpoint on this line. It needs an actual statement (not just a declaration such as 'int q;'), otherwise it moves your breakpoint down
}


and then binary search on the value of count to find where the bug occurs.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The platform sdk has a debug break function (int 3):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debugbreak.asp

Share this post


Link to post
Share on other sites

This topic is 4759 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this