rulururu

post Do not step into trivial functions

June 26th, 2008

Filed under: C++, Visual Studio — Kai @ 11:00 am

Today I’d like to explain to you how you can prevent the debugger from stepping into trivial functions. In my examples I use wxWidgets and its wxString. It should be almost the same if you use MFC and its CStrings.

If you do a lot of debugging (and who doesn’t?), you’ve probably encountered many function calls that you’d really like to step into. In particular, if the code makes function calls to produce arguments for the function:

call_me( new foo( "a", 1 ), my_vector.begin(), my_vector.end() );

When you are single stepping in the debugger (using F11) and you want to step into a function that has strings as a parameter, the debugger will first step into the string constructor. This is correct but annoying - especially when you are absolutely certain that you don’t need to. You don’t want to debug the string constructor. The same applies to many other functions, e.g. new(), malloc(), wxString::c_str(). There is a way to tell the debugger to step over these functions even if you use “Step Into”.

Create a ‘NoStepInto’ entry for every function you do not want to step into.

Note: Visual Studio uses the regular expression syntax to specify the function names. regexp is much more powerful than wildcards such as ? and *.

Note: Don’t forget - you have to “escape” special characters with ‘\’. If you want to step over wxString::c_str*, you need to write wxString\:\:c_str.*. There is a lot of confusion in the newgroups and other examples if you need to write \\:\\: or simply \:\:.. My personal view is, \: is enough (as is correct with regexp) and the other expression came from pasting a CString to a newgroup post.

If you are using Visual Studio 6, add these lines to autoexp.dat under

[ExecutionControl]
wxString\:\:c_str.*=NoStepInto
operator new=NoStepInto

If you are using Visual Studio .NET 2003 or 2002, modify the registry.

  • Add the Key NativeDE\StepOver under HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.1 (or 7.0 if you are using .NET 2002)
  • For every function you want to skip, add a new key/string value under the StepOver key.
  • The keys must be numerical. Think of them as linenumbers like in the old Basic days. Due to a bug in the Debugger, these numbers will get evaluated in reverse order.
  • The string contains the regular expression I have described above.

For instance, this is a good idea:

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\7.1\NativeDE\StepOver\10 = wxString\:\:c_str.*

This will prevent the debugger from stepping into these functions when you press F11 or choose “step into”.
Other useful expressions are:

  • wxString Constructor
  • wxString\:\:wxString
  • wxString Destructor
  • wxString\:\:~wxString

Visual Studio 8 has the same basic functionality as Visual Studio 7 with a few improvements. In addition, you should be aware that the key you must modify is now:

HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\8.0\NativeDE\StepOver

You’ll need an account with Admin privileges to set this up.

In addition to the ability to match regular expressions provided in Visual Studio 7, a few new escape sequences have been added to the regular expressions you can use, including '\funct' to match the name of a function, '\scope' to match a scope (e.g., std::my_namespace::etc::so::forth), and '\oper', which matches any operator.

Note: When using these escape sequences, you must indicate the end of the escape sequence with a colon. So you might write '\scope:' followed by the rest of the expression to match.

For example, you could avoid stepping into any overloaded operators by using the expression

\scope:operator\oper:=NoStepInto

which might be useful if you are using a lot of classes with simple operators whose behaviors you can trust to be correct.

There is a lot more you can do with it. Once done it saves time and energy ;)

post Correctly implement for() loop scooping

June 2nd, 2008

Filed under: C++, Visual Studio — Kai @ 2:13 pm

The following code snipped gives an error when compiling it with VC6, although it’s legal according to the C++ standart.

SomeFunction ()
{
    for (int i =0; i<5; i++)
    { ... }
    for (int i =0; i<10;i++)
    { ... }
}

Previously, the definition would be considered occurring outside of the braces, and would be in scope until the end of the outer enclosing braces (until the end of SomeFuncion() in the example above), so defining i in the second for loop gives a redefinition error.
Under the new rules, the variable definition is considered occurring inside the braces of the for() loop, and it goes out of scope at the end of the for loop, so both variable definitions given above would be needed.

It gives an error on the second for () complaining about i being redefined.

  • In VC7 (2002), the compiler was changed so that both conforming and
    non-conforming code was accepted, but neither interpretation was enforced -
    you could freely mix.
  • In VC 7.1 (2003), the /Zc:forScope option was introduced and was by-default
    turned off, preserving the VC7 (and earlier) behavior.
  • In VC8 (2005), the default was changed so that the option is on by default.
  • VC9 (2008) changes nothing with regard to for-loop variable scoping.

So, what do you do if you need the new scooping rules?

A tumb rule is that you can turn it off, and get the VC6 behavior, by adding /Zc:forScope- to
your compiler options in the project properties dialog.

The following macro has been suggested as a workaround:

#define for  if (0) {} else for

However, using a macro to change the semantics of a program is generally not a good idea, and should only be used in extreme cases, such as when trying to compile code developed under a compiler that implements the new rule. Applying the macro to code developed under the original rule can result in silent behavioral changes, as described above. A compiler that implements the new rule can warn about code whose interpretation differs under the old and new rules, so it’s best to wait for proper compiler support, if you can.

Another way you can go is to use the preprocessor macro MSC_VER and create conditional compilation blocks, or simply define the varibable outside of the first loop and set initialize it every time again.

Yet another workaround is to enclose each for() sentence in braces, like this:

{
    for ( int i = 0; i < 10; i++ )
    {
        //  ...
    }
}
{
    for ( int i = 0; i < 12; i++ )
    {
        //  ...
    }
}

This effectively creates yet another scope that hides the for-loop scooping bug. However, it adds unnecessary scopes and is easy to forget to close one of them or them alltogether.

post PowerCommands for Visual Studio

May 19th, 2008

Filed under: Visual Studio — Kai @ 10:58 am

PowerCommands 1.1 is a set of useful extensions for the Visual Studio 2008 adding additional functionality to various areas of the IDE. The source code is included and requires the VS SDK for VS 2008 to allow modification of functionality or as a reference to create additional custom PowerCommand extensions.

VS

Useful gimmicks are for instance opening of a Visual Studio Command Prompt, which has the right path to the current project on start. I really do like the “Undo Close” function, which helps you if you have clicked accidently a window into nirvana.

Below is a list of the included in PowerCommands:

  • Collapse Projects
  • Copy Class
  • Paste Class
  • Copy References
  • Paste References
  • Copy As Project Reference
  • Edit Project File
  • Open Containing Folder
  • Open Command Prompt
  • Unload Projects
  • Reload Projects
  • Remove and Sort Usings
  • Extract Constant
  • Clear Recent File List
  • Clear Recent Project List
  • Transform Templates
  • Close All

More about extending Visual Studio can be found Visual Studio Extensibility Development Center.

post SP 1 (Beta) for Visual Studio 2008 and .NET Framework 3.5

May 14th, 2008

Filed under: .NET, Visual Studio — Kai @ 1:33 pm

As I recently have experienced a few days ago the first beta version of the service pack 1 for Visual Studio 2008 and .NET Framework 3.5 have been released.

The release of the final version is announced for summer.

This servicing updates provide a roll-up of bug fixes and performance improvements.
Furthermore, additions and enhancements that make building .NET applications have become better.

The parts that provide development with C++/MFC won’t have many new features but C# development is said to be improved.

In any case you’d be aware of:

  • Users that have Windows Vista should have SP1 for Vista installed before upgrading the .NET 3.5 Framework with SP1.
  • Any users who still work with beta of Visual Studio 2008 (Codename Orcas I think) will get in trouble when trying to install the service pack. A detailed step-guide can be found here.
  • There is a change in behavior in the .NET 3.5 SP1 beta that causes a problem with the shipping versions of Expression Blend (WYSIWYG-Editor für XAML). The recommented way to work around this issue is to download the recently updated version of Blend 2.5.

At least bear in mind “be careful - it’s still beta form”. The usage on critical system is not recommented yet.

Scott Guthrie (Corporate Vice President der Microsoft Developer Division) has done a great blog post that shows the improvments in detail.
It can be found here.

The Visual Studio SP1 Beta is quite extensive and includes support for SQL Server 2008, new ADO.NET features, improvements to the WPF designers, WCF templates for Silverlight projects, debugger support for the .NET Framework public symbols and source release, control improvements and Visual C++ improvements such as the Office 2007 Ribbon bar.

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)