Menu

VisualCppHintsTips

Microsoft Visual C++ Express and Studio hints and tips

Checking for memory leaks

You may see, using Visual C++ warnings like:

First-chance exception at 0x12345678 in app.exe: 0xC0000001: Access
violation reading location 0x12345678.

First-chance exceptions can be seen even if the EXE does not crash and the Task Manager does not show any significant memory leaks. If a program uses exception handling then it can mean that the program successfully handled the exception.

The debugger is notified when these occur. It then passes it back to the program. If the program does not handle it, it returns to the debugger. This is a second-chance exception. Typically, a debugger will stop. If there is no debugger then the program will likely crash.

If you are concerned then you can set the Visual C++ debugger to break on first chance exceptions, via:

  • Select Debug => Exceptions...
  • Click Win32 Exceptions
  • Check Thrown beside the Access violation exception entry

Useful web pages

Running in Debug mode within Visual C++ with an application built using the /MTd compiler flag will cause lots of debugging information and memory leak info to be displayed e.g.

 {13983} normal block at 0x027844A8, 23 bytes long.
 Data: <C:/test.img > 43 3A 2F 61 61 61 61 61 61 61 61 2E 69 6D 67 00 
 {13982} normal block at 0x02784460, 23 bytes long.
 Data: <C:/test.hdr > 43 3A 2F 61 61 61 61 61 61 61 61 2E 68 64 72 00 
 {13981} normal block at 0x027841A0, 660 bytes long.
 Data: <            P   > 04 00 00 00 00 01 00 00 00 01 00 00 50 00 00 00

Debug versus Release mode

Visual C++ Express allows C++ to be compiled in Debug mode or Release mode.

  • In Debug mode additional debugging information is inserted into any executables.
  • Debug and Release modes are incompatible, so if you build a library in Release mode then other libraries dependent upon it must also be built in Release mode.

For example, in VTK, if you mix them up e.g. a Release mode VTK application and a Debug mode VTK library then you may get an error relating to vtkInformationStringKey e.g. see http://www.vtk.org/pipermail/vtkusers/2006-October/087761.html.

Or, for wxWidgets-related classes, you may get linker errors with code LNK1120: 1 unresolved externals.

For more information, see http://stackoverflow.com/questions/367884/debug-release-difference

For VTK, if you build in Debug mode then you need to beware if moving certain directories about e.g. Debug mode seems to assume that the VTK binary directory does not move.

Runtime library options

Static libraries have .lib extension, runtime/dynamic libraries have .dll extension.

If /MD is used you've built a runtime library. Applications using these assume that the DLL is in one of:

  • Same directory as application.
  • C:\Windows\system directory.
  • C:\Windows\system32 directory.
  • Application uses SetDllDirectory() to specify where they should be.

To create a standalone exe requires the /MT option to be set which means create a "multi-threading and a static library".

If you get an error like:

 This application has failed to start because the application configuration is incorrect.

This was because the program was compiled by default by Visual Studio using the /MD runtime library option which means "uses multi-threading and DLL runtime library" and means that msvcr80.dll must be available at runtime. VTK, by default, is also built using this option.

For more information, see

Beware pre-compiled headers

c:\test\test.cpp(21) : fatal error C1010: unexpected end of file 
while looking for precompiled header directive.

This can arise if the project expects pre-compiled headers and your code is missing an: #include"stdafx.h"

To enable/disable pre-compiled headers:

  • Right-click the project name in the Solution Explorer
  • Set the Configuration value to be Release
  • Click Configuration Properties
  • Click C/C++
  • Click Precompiled Headers
  • Toggle Create/Use Precompiled Header

Text encodings

Visual C++ projects can either be ANSI or Unicode. If there are problems then you will get an error like:

error C2664: ... cannot convert parameter 2 from 'constchar [4]' to 'LPCWSTR' 1>
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-stylecastor function-stylecast

You can change encoding by:

  • Right-click the project name in the Solution Explorer
  • Set the Configuration value to be Release
  • Click Configuration Properties
  • Click General
  • For property Character Set, use the drop-down list to select a value.
    • Select Use Unicode Character Set for Unicode
    • Select Use Multi-Byte Character Set for ANSI

Unicode is recommended

Editing resources

Visual C++ Express doesn't support editing of these so to create a resource file do:

  • Create a new .h file
  • Rename to .rc
  • Right-click the .rc file
  • Select Open with...
  • Select Source Code (Text) Editor

Runtime library options

To toggle between /MT and /MD do:

  • Right-click the project name in the Solution Explorer
  • Set the Configuration value to be Release
  • Click Configuration Properties
  • Click C/C++
  • Click Code Generation
  • For property Runtime Library, change it from /MD to /MT

In Visual C++ project files the values are:

  • RuntimeLibrary="0" for /MT
  • RuntimeLibrary="1" for /MTd
  • RuntimeLibrary="2" for /MD - default
  • RuntimeLibrary="3" for /MDd

Projects that create static libraries

Set the Properties => General => Configuration Type to Static Library (.lib).

To add the above as a pre-requisite in an other project:

  • In Properties => C/C++ => General => Additional Include Directories, add $(SolutionDir)\MyLib
  • In Properties => Linker => General => Additional Library Directories, add $(SolutionDir)\$(ConfigurationName)
  • In Properties => Linker => Input => Additional Dependencies, add MyLib.lib

Projects that use JPEG library

If using the JPEG library, you may need to edit the project properties to exclude the libc.lib library. To do this:

  • Right-click project name.
  • Click Configuration Properties
  • Click Linker
  • Click Input
  • In Ignore Specific Library, add libc.lib.

Related

Wiki: Home

Monday.com Logo