How to debug a function in release mode

29 07 2015


The Visual Studio IDE provides different optimization options that help us to create code for maximum speed or minimum size. Mostly we select /O1 optimizes code for minimum size when we build our project in release configuration. But you might be observed that the variable values may not be displayed correctly in the watch window if we try to debug our source code in release configuration. Of-course, we have options to enable debugging support in release mode. But just think in case if you want to debug just one function in release mode!!!

Pragma directives specify machine- or operating-specific compiler features. The compiler optimization can be performed on a function-by-function basis using #prgma optimize statement.

#pragma optimize( "[optimization-list]", {on | off} )

We can disable the optimization using the optimize pragma with the empty string (“”).

#pragma optimize( "", off )
void FunctionToDebug()
{
}
#pragma optimize( "", on ) 


Some pragmas provide the same functionality as compiler options. When a pragma is encountered in source code, it overrides the behavior specified by the compiler option.

Advertisements




dynamic_cast throws unhandled exception in VC++ 6.0

17 05 2013


Usually we upgrade our projects to higher versions of Visual Studio IDE for getting latest Windows libraries and making our project to be compatible with latest technologies. But recently we had to downgrade one of our DLLs to VC++ 6.0 from VS2005. As first step, we created new project in VC++ 6.0, resolved all compiler errors and successfully compiled it in VC++ 6.0 IDE. But when we integrated it with client application, it started throwing unhandled exception from wherever it used dynamic_cast. 😦


We have to explicitly enable Run-Time Type Information(RTTI) for our project in VC++ 6.0 compiler. In Visual C++ 2005, RTTI is on by default.

Enabling RTTI in VC++ 6.0 Project,

1) Open the project’s Property Page

2) Click the C/C++ Tab

3) Select C++ Language Category

4) Select Enable Run-Time Type Information (RTTI) checkbox

RTTI


typeid operator also fail if we don’t enable RTTI in our project.





How to convert a ANSI/MB project into a UNICODE

14 05 2013

Most of the time we may need to give multiple language support for our applications. Incase if we need to support Russian or Chinese language means, definitely we have to give Unicode support for our application. So here I would like to share some tips for converting existing ANSI/MultiByte project into Unicode.

If we build our project in Multi-byte character set it will allocate 1 byte for one character. This is enough for representing all English letters. But if we build our project in Unicode character set it will allocate 2 bytes for each letters. It is required for representing Russian and Chinese letters.

Below are some suggestions for converting MBCS to Unicode project

Step 1: Create new configuration from Project Configuration Manager. Give name something like ReleaseUnicode/DebugUnicode.

Step 2: Change Character Set to Unicode from Project Settings.

Unicode

Step 3: Replace all char datatype with TCHAR. This will automatically switch to normal char or wchar_t based on it is compiled as MBCS or Unicode respectively.

Step 4: Add _T as prefix to each string text. eg: _T(“Hello World”)

Step 5: Replace LPSTR and LPCSTR with LPTSTR and LPCTSTR respectively.

Step 6: Replace following win32 APIs with corresponding Generic equivalent for same.

ANSI/MB API Generic API
strstr _tcsstr
strcmp _tcscmp
sprintf_s _stprintf_s
strlen _tcslen
_stricmp _tcscmp
strcpy_s _tcscpy_s
atol _tstol
atoi _tstoi
atof _tstof
strcspn _tcscspn
fopen_s tfopen_s
strtok_s _tcstok_s
strtol _tcstol
strtoul _tcstoul




How to sign C++ assemblies with a strong name

21 10 2012

Strong Name (SN) is a technology introduced with the .NET platform, which gives a globally unique identity to the applications or components. If our application is signed with a strong name, then it is mandatory to sign all assemblies it refer.

In case if you are working in COM dll or Managed C++ library, you may need to provide signed version of your DLL to client applications.

First step is to create a key file using sn.exe tool which we can find out with .NET Framework SDK,

sn.exe -k [Directory]\MyKey.snk

Sign COM Interop DLLs

If we are working on COM dlls, generally we generate ineterop DLLs too for .NET client applications. This can be generated by tlbimp.exe.
Tlbimp.exe provides the functionality to set a /keyfile: as an argument to generate the interop dll which is signed with a Strong Name.

The following commands shall be added to the project’s post build events.

tlbexp MyComDll.dll /out:MyComDll.tlb
tlbimp MyComDll.tlb /keyfile:MyKey.snk /namespace:Company.MyComDll /output:Interop.MyComDll.dll

Sign Managed C++ library

A managed DLL can be signed by specifying the key file in visual studio project settings at Project-> Properties-> Configuration Properties-> Linker-> Advanced-> Key File

We can check whether our DLLs are signed or not using same SN.EXE tool. The following command will give key information.

sn.exe -T MySignedDll.dll




How to debug a release build?

7 01 2012

To enable debugging of a release build, change the following release build options.

  1. Open solution’s Property Pages dialog.
  2. In the C/C++ -> Optimization, select Disabled (/Od).
  3. In the C/C++ -> General -> Debug Information Format, select Program Database (/Zi).
  4. In the Linker -> Debugging -> Generate Debug Info , select Yes (/DEBUG).

After doing this, rebuild and test your program. You can now debug your release build application. 🙂





Visual Studio IDE – Format Document

19 01 2009

description8

 

 

When we write large line of codes it is necessary to keep the readability of the code. The curly braces “{}” should be arranged in proper order, tab spacing should be same for the codes within a pair of opening and closing braces and etc. etc.

In Visual Studio there is a feature called Format Document which will align the code systematically.  There have different methods to do this. I am going to describe one of them.howcanidoit16

 

 

The following method mentions how to align the code within a pair of curly braces. For that first of all place the cursor just before or after the one of the curly braces ( “{” or ” }” ). Then press the key combination Ctrl+Shift+}, now the codes enclosing the pair of curly braces become selected. Then press the key combination Alt+F8. Now the code looks pretty organized.right? 🙂

Suppose our code looks like,

fomatdocs1

 

Press the key combinations, now the code looks aligned 🙂
 

 

fomatdocs2

 

mynote7

 

 

If you want to format only a selected section of the code.  Select the  area you want to format and press Alt+F8.