In this blog, I’ll demonstrate on how to utilize Address Sanitizer (ASan) in Visual C++ to check for memory problems. MSVC team ported the Clang ASan to the Windows platform in 2019 and since it is still at the experimental stage, be sure to expect kinks to be ironed out.
Before using Address Sanitizer in Visual C++, it has to be installed by the Visual Studio Installer. Check the “C++ AddressSanitizer (Experimental)” checkbox and then click the Modify button.
After installing ASan, be sure to add this path to your PATH environment variable so that your executable can find the clang_rt.asan_dynamic-i386.dll
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x86
Add the environment variable by clicking the “New” button and paste the path in the new row and click Ok.
Enable the AddressSanitizer in your C++ project properties.
At this time of writing, ASan supports only Release and 32-bit build. x64 support is in the works and is coming soon. As a matter of fact, x64 support is available on Visual C++ Update 16.7 Preview 1.
Add the 2 lines of code below to the main function to trigger the ASan detection of memory access violation and console application would terminate to show the line number of the source code that causes this crash. As the console output is verbose, it shall not be shown here.
int* arr = new int; arr = 1;
Attempt to build MFC application with ASan causes these multiple defined symbols linker errors because new and delete operators are defined in MFC and Clang library at the same time. I have no good way of resolving this linkage errors. I have filed a bug report, please help upvote it else Microsoft will unilaterally close it without resolving it.
2>uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj) 2>uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj) 2>uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??_U@YAPAXI@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj) 2>uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??_V@YAXPAX@Z) already defined in clang_rt.asan_cxx-i386.lib(asan_new_delete.cc.obj) 2>D:\TextPerfect\Source\Release\SDIScratchPad2.exe : fatal error LNK1169: one or more multiply defined symbols found
Win32 OpenGL application
Lastly, I tried ASan on a Win32 OpenGL application. To make sure ASan works as intended, I added the 2 offending lines. After I made sure ASan detection works, I remove those 2 lines.
int* arr = new int; arr = 1;
To see the ASan output in a GUI application which does not have a console, you have use Visual Studio to debug your Release build application. The ASan output will be shown in the output pane of Visual Studio. But you will notice no line number of the offending line is revealed. To fix that, let’s add debug information to your Release build.
This step is automatically done for the earlier console application. As long as the OpenGL application runs to completion without crashing, it means ASan is not triggered on memory access violations.