Archive

Posts Tagged ‘Visual studio’

Breakpoints in Visual Studio

May 2, 2009 4 comments

What’s a breakpoint?

A breakpoint is defined as the location where a debugger breaks execution to allow the user to have a look or to modify the execution context.

What’s new with breakpoints?

With visual studio 2005 and 2008 behavior of breakpoint has changed. Some features that were added are as follows…

  1. Know hit count of a break point, no more need to keep a temp debugger variable for counting hits. For e.g. you can set a conditional breakpoint and then enable hit count. You’ll see how many times the condition was satisfied, you can also disable breaking of execution so that the program keeps running.
  2. Trace local variables/function name and more to the visual studio immediate/output window.
  3. Run a macro when a breakpoint is hit.
  4. We can disable breaking of execution which means there will only be tracing going on and no breaking of execution.

So to access these features, after adding a breakpoint, open the breakpoint window and right click on this breakpoint. Lower half of the dropdown contains these features, here is a screenshot of the context menu…

Breakpoint Context Menu

Breakpoint Context Menu

There are different types of break points available in visual studio. There are four in my knowledge…

  1. File breakpoint – Breaks at a location in a file
  2. Address breakpoint – Breaks at an address
  3. Function breakpoint – Breaks at a function
  4. Data breakpoint – Breaks at specific byte locations

Hit count

Let me show you how to count the number of even numbers from 1 to 100, I know it’s quite easy, it’s fifty, but via debugger? Here is a small function with a loop from 1 to 100.

void CountEven( const int From, const int To )
{
   for( int Index = From; Index < To; ++Index )
   {
      // Set a conditional breakpoint to get the count of odd numbers,
      // Dummy code to allow set a breakpoint
      ::SendMessage( AfxGetMainWnd()->GetSafeHwnd(), WM_NULL, 0, 0 );
   }
}

Now set a break point inside the for loop (Press F9) and right click and select “Breakpoint->Condition” item. You’ll get the following dialog…

Setting a Conditional breakpoint

Setting a Conditional breakpoint

So here I’ve set the condition that whenever Index%2 is zero then we have an even number. So when this happens I’m asking the debugger to break execution. So this works as expected but our aim is to count the number of even numbers using debugger, so for that again right click on the breakpoint line and select “Breakpoint->Hit Count” item, following dialog pops up…

Enable hit count in the debugger

Enable hit count in the debugger

You can see that I’ve selected an option called “break when the hit count is a multiple of”. I’ve given 10 as the option, so that frequency of breaking of execution is less. When I run the code this is what I get in the breakpoints window…

Hit count result

Hit count result

See the “Hit Count” item?  Execution broke 5 times this means for every 10 hits, hence count is 50. Isn’t this fun?

Tracing

Remember the times when we had to trace statements just for debugging purpose, for e.g. we wanted to trace a certain variable’s value when it satisfies a condition. Let’s take the above example, we’ve already enabled a conditional breakpoint such that it’s breaks execution whenever “Index” is an even number. Right click on the breakpoint line and select “Breakpoint” and then “When Hit” option. Following dialog pops up…

When Hit Dialog

When Hit Dialog

Check the first option Print a message”, this is the option that prints a message either to your “immediate window” or to your “Output window” based on the options that you’ve set. Now run the program to have some fun, this is the output in my computer…

Output of "when hit"

Output of "when hit"

All even index values are traced to the output window, isn’t this cool?

Filter

This one is even more cool. It allows us to set breakpoint filters. This is kind of a conditional but with a larger scope. For e.g. if you want to break execution only for a certain thread or only for a certain process. So for this right click on the breakpoint line and select “Breakpionts->Filter”. Following dialog pops up…

Breakpoint filter

Breakpoint filter

Pretty easy to use. So this means for a function that is called from multiple threads you can explicitly tell the debugger for which threads to break execution.

Tid bits

  1. Did you know that you can set a breakpoint in the call stack window by press F9?
  2. Did you know that you can delete all breakpoints by press Ctrl + Shift + F9?
  3. Did you know that you can run to cursor by pressing Ctrl + F10?
  4. Did you know that you display breakpoints window by pressing Alt + F9?
  5. Did you know that you can use “Set Next Statement” by pressing Ctrl + Shift + F10?

Yeap that’s it from me for now, all the best. 🙂

Script error in VC++ wizards after installing IE8

April 1, 2009 3 comments

You might have script errors in VC++ wizards after installing IE8, for e.g. when you double click inside a dialog normally we should taken to corresponding file but instead we get a script error…

Script error

Script error

Pretty strange isn’t it. Well as per VC++ team blogs this bug due to IE8 installation. Reason for this behavior is as explained below (quoted from VC++ team blogs…)

The VC++ Wizards Engine implements the IInternetSecurityManager interface.  In this implementation it allows or disallows specific actions under certain policies that Internet Explorer queries it about.  In IE8 a custom Security Manager now also gets queried about the URLACTION_ACTIVEX_OVERRIDE_REPURPOSEDETECTION policy which IE previously did not delegate to custom security managers when the engine wasn’t running in the iexplore.exe process.  The IE engine then fails this action because we don’t have a policy entry for it in the custom zone for VC++ Wizards. We are still investigating whether this change in IE8 is by design and will possibly be addressing it by a fix in either the Wizard or IE components depending on the outcome.

And according to this blog, following fix should work….

Please follow the following steps:
  • Open regedit (on a 64-bit OS, open the 32-bit regedit)
  • Under “HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet
    Settings\Zones”, create a new key called 1000 (if it isn’t already there)
  • Under 1000, create a DWORD entry with:
    • o Name = 1207
    • o Type = REG_DWORD
    • o Data = 0x000000

Just now I had this problem hence I had to use this fix and it works. 🙂

Visual studio shortcuts that I use

Some useful shortcuts for development in different versions of visual studio for VC++…

  1. Ctrl + Alt + L – Solution explorer
  2. Alt + Enter – Properties window
    Works even on open files in the IDE. Lists the properties of files like full path etc. If in resource editor then it shows properties of controls
  3. Ctrl + Shift + F8 – Helps in selecting blocks of text using keyboard navigation keys, press escape to break out of this behavior
  4. Alt + 0 – VC6 – Shows workspace window
  5. Ctrl + Pagedown/Pageup – To navigate through the tabs in the output window or the workspace window. Works even in excel to navigate through worksheet tabs
  6. Ctrl + Shift + E – Opens resource explorer window
  7. Ctrl + Shift + C – Opens class view
  8. Ctrl + Shift + B – To start building
  9. Ctrl + F8 – VC6 – Turns on sticky shift, enters selection mode by selecting current line, use arrow keys to move selection up or down. Press escape when done
  10. Ctrl + Alt + O – Open output window
  11. To navigate through errors, use F8 in VS7 and above and F4 in VS6.
  12. Shortcut heaven in VC6, if interested to learn new shortcuts goto this dialog and the marked place. Press some shortcut keys to see what they are meant for. This is how I learn about new shortcuts in Microsoft products, it’s a standard feature in nearly all of their products. Goto Tools->Customize.
    Navigate through the category combo and command list to find out about new shortcuts. If you are using VC6 you can get a list of existing shortcuts using Help->Keyboard Map, take a print out stick it in your cabin, every now and then have a look at the list and implement these one by one. Shortly you won’t using mouse too much, will save time. :)This dialog is found in VS6 and equivalent one exists in some or the other form in other versions of visual studio too. So have fun learning these shortcuts.

Variable value is always’s zero, no matter what you do!

May 8, 2008 3 comments

Recently a friend of mine (sitting just behind me) had a strange problem. I wrote an inline function which was like…

void SetBlah( const bool IsBlah ) { m_IsBlah = IsBlah; }

Now he was using this function since we work for the same project! So he had a value 1 for IsBlah when calling this function but no matter what he did m_IsBlah always had the value zero!

He spent half a day trying to solve this issue, I too joined him in this venture. We thought it has got something to do with inlining the function call so we made it non inlined but this didn’t solve the issue. No matter what we did still the value was zero, we tried rebuilding the project but no effect whatsoever.

I tried running the code on my machine and it was fine, working as it should be!

So in the end I found out the problem? But before I post the answer here, any idea what could be the reason?

Answer

He had a statement in his watch window like…

theStaticLongNameObject.m_IsBlah = 0;

This expression prevented the value from changing. Also we can’t see the full expression in watch window because of the variable’s long name! 🙂

%d bloggers like this: