<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to VisualCppHintsTips</title><link>https://sourceforge.net/p/bric1936/wiki/VisualCppHintsTips/</link><description>Recent changes to VisualCppHintsTips</description><atom:link href="https://sourceforge.net/p/bric1936/wiki/VisualCppHintsTips/feed" rel="self"/><language>en</language><lastBuildDate>Wed, 10 Apr 2013 10:30:16 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/bric1936/wiki/VisualCppHintsTips/feed" rel="self" type="application/rss+xml"/><item><title>VisualCppHintsTips modified by Mike Jackson</title><link>https://sourceforge.net/p/bric1936/wiki/VisualCppHintsTips/</link><description>&lt;div class="markdown_content"&gt;&lt;h1 id="microsoft-visual-c-express-and-studio-hints-and-tips"&gt;Microsoft Visual C++ Express and Studio hints and tips&lt;/h1&gt;
&lt;h2 id="checking-for-memory-leaks"&gt;Checking for memory leaks&lt;/h2&gt;
&lt;p&gt;You may see, using Visual C++ warnings like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;First&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;chance&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; 0&lt;span class="n"&gt;x12345678&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 0&lt;span class="n"&gt;xC0000001&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Access&lt;/span&gt;
&lt;span class="n"&gt;violation&lt;/span&gt; &lt;span class="n"&gt;reading&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt; 0&lt;span class="n"&gt;x12345678&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;If you are concerned then you can set the Visual C++ debugger to break on first chance exceptions, via:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select Debug =&amp;gt; Exceptions...&lt;/li&gt;
&lt;li&gt;Click Win32 Exceptions&lt;/li&gt;
&lt;li&gt;Check Thrown beside the Access violation exception entry&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Useful web pages&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx"&gt;http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.codeguru.com/forum/showthread.php?t=312742"&gt;http://www.codeguru.com/forum/showthread.php?t=312742&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt; &lt;span class="p"&gt;{&lt;/span&gt;13983&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;normal&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; 0&lt;span class="n"&gt;x027844A8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 23 &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="n"&gt;long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
 &lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 43 3&lt;span class="n"&gt;A&lt;/span&gt; 2&lt;span class="n"&gt;F&lt;/span&gt; 61 61 61 61 61 61 61 61 2&lt;span class="n"&gt;E&lt;/span&gt; 69 6&lt;span class="n"&gt;D&lt;/span&gt; 67 00 
 &lt;span class="p"&gt;{&lt;/span&gt;13982&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;normal&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; 0&lt;span class="n"&gt;x02784460&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 23 &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="n"&gt;long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
 &lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hdr&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 43 3&lt;span class="n"&gt;A&lt;/span&gt; 2&lt;span class="n"&gt;F&lt;/span&gt; 61 61 61 61 61 61 61 61 2&lt;span class="n"&gt;E&lt;/span&gt; 68 64 72 00 
 &lt;span class="p"&gt;{&lt;/span&gt;13981&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;normal&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; 0&lt;span class="n"&gt;x027841A0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 660 &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="n"&gt;long&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
 &lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;            &lt;span class="n"&gt;P&lt;/span&gt;   &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 04 00 00 00 00 01 00 00 00 01 00 00 50 00 00 00
&lt;/pre&gt;&lt;/div&gt;


&lt;h2 id="debug-versus-release-mode"&gt;Debug versus Release mode&lt;/h2&gt;
&lt;p&gt;Visual C++ Express allows C++ to be compiled in Debug mode or Release mode.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In Debug mode additional debugging information is inserted into any executables.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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 &lt;a href="http://www.vtk.org/pipermail/vtkusers/2006-October/087761.html."&gt;http://www.vtk.org/pipermail/vtkusers/2006-October/087761.html.&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Or, for wxWidgets-related classes, you may get linker errors with code LNK1120: 1 unresolved externals.&lt;/p&gt;
&lt;p&gt;For more information, see &lt;a href="http://stackoverflow.com/questions/367884/debug-release-difference"&gt;http://stackoverflow.com/questions/367884/debug-release-difference&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2 id="runtime-library-options"&gt;Runtime library options&lt;/h2&gt;
&lt;p&gt;Static libraries have &lt;code&gt;.lib&lt;/code&gt; extension, runtime/dynamic libraries have &lt;code&gt;.dll&lt;/code&gt; extension.&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;/MD&lt;/code&gt; is used you've built a runtime library. Applications using these assume that the DLL is in one of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Same directory as application. &lt;/li&gt;
&lt;li&gt;&lt;code&gt;C:\Windows\system&lt;/code&gt; directory. &lt;/li&gt;
&lt;li&gt;&lt;code&gt;C:\Windows\system32&lt;/code&gt; directory. &lt;/li&gt;
&lt;li&gt;Application uses &lt;code&gt;SetDllDirectory()&lt;/code&gt; to specify where they should be. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To create a standalone exe requires the &lt;code&gt;/MT&lt;/code&gt; option to be set which means create a "multi-threading and a static library".&lt;/p&gt;
&lt;p&gt;If you get an error like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt; &lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;because&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;incorrect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This was because the program was compiled by default by Visual Studio using the &lt;code&gt;/MD&lt;/code&gt; 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.&lt;/p&gt;
&lt;p&gt;For more information, see&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://stackoverflow.com/questions/757418/should-i-compile-with-md-or-mt"&gt;http://stackoverflow.com/questions/757418/should-i-compile-with-md-or-mt&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.paraview.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F"&gt;http://www.paraview.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="beware-pre-compiled-headers"&gt;Beware pre-compiled headers&lt;/h2&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;\&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="o"&gt;\&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;21&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fatal&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="n"&gt;C1010&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;unexpected&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; 
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;looking&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;precompiled&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="n"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This can arise if the project expects pre-compiled headers and your code is missing an: &lt;code&gt;#include"stdafx.h"&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;To enable/disable pre-compiled headers:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Right-click the project name in the Solution Explorer &lt;/li&gt;
&lt;li&gt;Set the Configuration value to be Release &lt;/li&gt;
&lt;li&gt;Click Configuration Properties &lt;/li&gt;
&lt;li&gt;Click C/C++ &lt;/li&gt;
&lt;li&gt;Click Precompiled Headers &lt;/li&gt;
&lt;li&gt;Toggle Create/Use Precompiled Header &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="text-encodings"&gt;Text encodings&lt;/h2&gt;
&lt;p&gt;Visual C++ projects can either be ANSI or Unicode. If there are problems then you will get an error like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="n"&gt;C2664&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="n"&gt;cannot&lt;/span&gt; &lt;span class="n"&gt;convert&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt; 2 &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s"&gt;'constchar [4]'&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="s"&gt;'LPCWSTR'&lt;/span&gt; 1&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Types&lt;/span&gt; &lt;span class="n"&gt;pointed&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="n"&gt;unrelated&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;conversion&lt;/span&gt; &lt;span class="n"&gt;requires&lt;/span&gt; &lt;span class="n"&gt;reinterpret_cast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;stylecastor&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;stylecast&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can change encoding by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Right-click the project name in the Solution Explorer &lt;/li&gt;
&lt;li&gt;Set the Configuration value to be Release &lt;/li&gt;
&lt;li&gt;Click Configuration Properties &lt;/li&gt;
&lt;li&gt;Click General &lt;/li&gt;
&lt;li&gt;For property Character Set, use the drop-down list to select a value. &lt;ul&gt;
&lt;li&gt;Select Use Unicode Character Set for Unicode &lt;/li&gt;
&lt;li&gt;Select Use Multi-Byte Character Set for ANSI &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Unicode is recommended&lt;/p&gt;
&lt;h2 id="editing-resources"&gt;Editing resources&lt;/h2&gt;
&lt;p&gt;Visual C++ Express doesn't support editing of these so to create a resource file do:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create a new &lt;code&gt;.h&lt;/code&gt; file &lt;/li&gt;
&lt;li&gt;Rename to &lt;code&gt;.rc&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Right-click the &lt;code&gt;.rc&lt;/code&gt; file &lt;/li&gt;
&lt;li&gt;Select Open with... &lt;/li&gt;
&lt;li&gt;Select Source Code (Text) Editor &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="runtime-library-options_1"&gt;Runtime library options&lt;/h2&gt;
&lt;p&gt;To toggle between &lt;code&gt;/MT&lt;/code&gt; and &lt;code&gt;/MD&lt;/code&gt; do:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Right-click the project name in the Solution Explorer &lt;/li&gt;
&lt;li&gt;Set the Configuration value to be Release &lt;/li&gt;
&lt;li&gt;Click Configuration Properties &lt;/li&gt;
&lt;li&gt;Click C/C++ &lt;/li&gt;
&lt;li&gt;Click Code Generation &lt;/li&gt;
&lt;li&gt;For property Runtime Library, change it from &lt;code&gt;/MD&lt;/code&gt; to &lt;code&gt;/MT&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In Visual C++ project files the values are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;RuntimeLibrary="0"&lt;/code&gt; for &lt;code&gt;/MT&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RuntimeLibrary="1"&lt;/code&gt; for &lt;code&gt;/MTd&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RuntimeLibrary="2"&lt;/code&gt; for &lt;code&gt;/MD&lt;/code&gt; - default &lt;/li&gt;
&lt;li&gt;&lt;code&gt;RuntimeLibrary="3"&lt;/code&gt; for &lt;code&gt;/MDd&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="projects-that-create-static-libraries"&gt;Projects that create static libraries&lt;/h2&gt;
&lt;p&gt;Set the Properties =&amp;gt; General =&amp;gt; Configuration Type to Static Library (.lib).&lt;/p&gt;
&lt;p&gt;To add the above as a pre-requisite in an other project:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In Properties =&amp;gt; C/C++ =&amp;gt; General =&amp;gt; Additional Include Directories, add &lt;code&gt;$(SolutionDir)\MyLib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;In Properties =&amp;gt; Linker =&amp;gt; General =&amp;gt; Additional Library Directories, add &lt;code&gt;$(SolutionDir)\$(ConfigurationName)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;In Properties =&amp;gt; Linker =&amp;gt; Input =&amp;gt; Additional Dependencies, add &lt;code&gt;MyLib.lib&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="projects-that-use-jpeg-library"&gt;Projects that use JPEG library&lt;/h2&gt;
&lt;p&gt;If using the JPEG library, you may need to edit the project properties to exclude the &lt;code&gt;libc.lib&lt;/code&gt; library. To do this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Right-click project name. &lt;/li&gt;
&lt;li&gt;Click Configuration Properties &lt;/li&gt;
&lt;li&gt;Click Linker &lt;/li&gt;
&lt;li&gt;Click Input &lt;/li&gt;
&lt;li&gt;In Ignore Specific Library, add &lt;code&gt;libc.lib&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Mike Jackson</dc:creator><pubDate>Wed, 10 Apr 2013 10:30:16 -0000</pubDate><guid>https://sourceforge.net91fbf9788c60c209e7ee21057a313d4e766a9eab</guid></item></channel></rss>