Tag: visual studio

Entries for tag "visual studio", ordered from most recent. Entry count: 48.

Pages: 1 2 3 ... 6 >

23:57
Wed
14
Jan 2015

Review: Deleaker - A tool that finds resource leaks

Deleaker is a tool for programmers that finds resource leaks in C++ programs. It's commercial, with free trial and unconditional 30 day money back. Here is my review of this tool. I've tested version 3.0.27.0.

Deleaker is installed as a plugin for Visual Studio, any version from 2005 to 2013. It also works with Visual Studio Community 2013, as this new free version also supports plugins. There is also standalone Deleaker application (see below).

The purpose of this tool is to augment debugging of native C++ programs with the ability to list all resources that are allocated at the moment (heap memory, virtual memory, OLE memory, GDI objects, USER objects, handles) and so to detect resource leaks. Here is how it works:

The interface is very simple - it can be learned in just few minutes. You can build your program and start debugging it by hitting F5, Deleaker is enabled automatically. Now just open dedicated panel (menu Deleaker > Deleaker Window) and there press "Take snapshot" button. You don't even have to pause execution, but of course the button works as well when your program is paused at a breakpoint. After few seconds, the panel is populated with a list of currently allocated resources, with the place from which it was allocated shown in first column.

After selecting one, bottom panel displays full call stack. Clicking in this call stacks navigates to the place in the source code where the allocation is specified. Finally, after program exit, the list is filled with resources that were not freed - these are actual leaks!

You can filter the list by module (EXE or DLL file that made the call) and by resource type (memory, GDI objects etc.). There is also a column with size of the resource and "Hit Count" - number of resources that were allocated by that particular place in the code (e.g. inside a loop) and stay allocated at the moment.

"Show full stack" button is a nice feature. Clicking it displays full call stack, while by default, the stack is stripped from entries that don't come from your code, but from system libraries. For example, above my function with the actual allocation instruction, there is MSVCR120D.dll!operator new, then there is MSVCR120D.dll!malloc etc... until ntdll.dll!RtlAllocateHeap. It's good that the program can ignore such call stack entries. It also entirely ignores allocations made by system modules outside of your code.

Unfortunately it does this only by identifying module that the function comes from and not it's name, so it cannot ignore templates, like these from STL containers. Maybe ignoring functions by name specified as wildcard or regular expression would help, e.g. "std::*" or "std\:\:.+" - just like Visual Studio debugger can step over specified functions, as I described in How to Make Visual Studio Debugger not Step Into STL.

You can press "Take snapshot" multiple times and save the snapshots for later view. (They are just numbered, you cannot give them names.) By the way, Deleaker captures F5 key, so even when during debugging session, if the focus is in Deleaker panel, this button doesn't resume your program, but instead refreshes the list of allocations (takes new snapshot). You can also select two snapshots and compare them. Then you see only resources that were allocated in the right snapshot and not in the left, which can indicate a leak that happened during some time of the program execution.

Besides heap memory allocations, the tool can also detect other types of resources, like GDI objects. Unfortunately not all interesting types of resources are covered. For example, an opened file of type FILE* f = fopen(...) is shown as normal memory allocation and opened file of type HANDLE f = CreateFile(...) is not shown at all, but I guess it must be due to some system internals.

I didn't find a single leak in my main home project, so I created a dedicated, simple program to test if it can really find leaks. I also checked that it works with programs compiled in Release configuration as well.

Aside from being a Visual Studio plugin, Deleaker can also work as standalone Windows application.

Overall, I like the program. If its price is not a problem for you or your company, I think it can be very useful in improving quality of developed software. I especially like the fact that it's so easy to learn and use.

Comments (0) | Tags: c++ tools visual studio | Author: Adam Sawicki | Share

23:47
Thu
13
Nov 2014

Microsoft Visual Studio Community 2013 !!!

Microsoft just released a new edition of Visual Studio that will replace Express. It's called Visual Studio Community 2013 and it's free, while it has all the features of the commercial edition, including support for plugins and many advanced tools. You can download it from Visual Studio Downloads, web installer or DVD ISO. It works with "old" Windows 7 as well. Good move Microsoft! For me it's probably the news of the month :) So far, I installed the IDE and tested on my home project CPU profiling:

...and Graphics Analyzer, which replaces good old PIX:

Comments (0) | Tags: visual studio | Author: Adam Sawicki | Share

14:49
Sun
28
Sep 2014

Building 64-bit Executables in Visual Studio Express

I admit that for years I was convinced only the paid version of Visual Studio can compile 64-bit EXE files. It's just because I never really needed to create one in my personal projects. Now I have to do it (what for? - I will show in one of my next posts) and as it turns out, it's perfectly possible and easy to do also in Visual Studio Express. You just have to make some configuration because the "x64" platform is not shown by default. So to compile 64-bit executable in your Visual Studio 2013 Express:

You can now choose and build any configuration (Debug or Release) for any platform (Win32 or x64). For x64 platform, intermediate and output files will be written to subdirectories: x64\Debug\ and x64\Release\. Notice that for Win32, it's just Debug\ and Release\.

Comments (3) | Tags: c++ visual studio | Author: Adam Sawicki | Share

19:14
Tue
23
Apr 2013

Type Visualization in Visual Studio 2012 Debugger

When you code in C++ and you have your own library of data types, especially containers, it would be nice to be able to see it in the debugger formatted in some readable way. In Visual C++/Visual Studio, there used to be a special file autoexp.dat designed for this purpose, as described in "Writing custom visualizers for Visual Studio 2005". But it had weird syntax and poor error reporting.

Now in Visual Studio 2012 there is a new way of defining debugger visualizations for native data types, called Native Type Visualization Framework (natvis). All you need to do is to create an XML file with ".natvis" extension following special format and place it in directory: %USERPROFILE%\Documents\Visual Studio 2012\Visualizers. Full documentation of this format is on this single MSDN page: "Creating custom views of native objects in the debugger". See also "Expressions in Native C++" and "Format Specifiers in C++".

For example, if you have a singly linked list:

template<typename T>
class CLinkedList {
   // ...
    struct CNode {
       T Value;
       CNode* Next;
    };
    size_t Count;
    CNode* Head;
};

Default visualization of a 3-element object in the debugger would look like this:

But if you create following natvis file:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
   <Type Name="CLinkedList&lt;*&gt;">
       <DisplayString>{{Count = {Count}}}</DisplayString>
       <Expand>
           <Item Name="[Count]">Count</Item>
           <LinkedListItems>
               <Size>Count</Size>
               <HeadPointer>Head</HeadPointer>
               <NextPointer>Next</NextPointer>
               <ValueNode>Value</ValueNode>
           </LinkedListItems>
       </Expand>
   </Type>
</AutoVisualizer>

Next time you start debugging (restarting Visual Studio is not required), same object will be shown as:

Besides extracting single fields from objects, evaluating whole C++ expressions and formatting values into a string for summary of whole object, this framework is able to visualize following data structures:

Comments (5) | Tags: visual studio debugger c++ | Author: Adam Sawicki | Share

23:03
Sun
27
May 2012

How to Run Windows Command with Given Working Directory?

The concept of "working directory" during program startup or "current directory" of the running process is an interesting topic in itself. Maybe I'll write another time about how to manipulate it in different programming languages. This time enough to say that some programs look for auxilliary files in working directory, some in the directory where own EXE file is located and some in other places like user's profile directory.

Problems begin when a program needs auxilliary files located in same directory as executable, but uses working directory to locate it. Apparently such program expects to always be ran with working directory equal to path of its EXE. It happened to me yesterday while using Windows port of Bison (parser generator). Error was:

win_bison: cannot open file `data/m4sugar/m4sugar.m4': No such file or directory

I can't just run the program with another working directory because I execute it from Visual C++, as a Custom Build Tool associated with my ".y" file. There is only place to enter a command in file property page, no place to change working directory, which is by default the directory of Visual C++ project I think.

The solution I found to be able to run a console command with given parameters and also with given working directory is to do it indirectly, using system "start" command, like this:

start /B /WAIT /D <WorkingDir> <ExePath> <Parameters>

Update 2012-11-29: I was informed that the problem in win_bison is now fixed so it can be used without such workaround.

Comments (17) | Tags: visual studio windows | Author: Adam Sawicki | Share

20:48
Mon
16
Apr 2012

Redirecting Output to File in Visual C++

If you write a console program in C++ that prints
 a lot of text to the standard output, sometimes watching these few newest line that appear in the black system console is not enough. When running the program from command line, you can redirect its output to a text file with syntax like > Output.txt.

What about launching program from Microsoft Visual Studio / Visual C++ Express? Can you redirect console output to file from there? There is no field for it in project properties, but it actually works when you just append the redirect symbol to Command Arguments field, like this:

Comments (36) | Tags: c++ visual studio | Author: Adam Sawicki | Share

17:00
Sat
14
Apr 2012

unique_ptr in Visual C++ 2010

Sure C++ doesn't have garbage collector, but the way of freeing memory and other resources recommended for this language is RAII idiom - creating classes (like smart pointers) that free pointed object in destructor. Standard library of old C++ provided only auto_ptr class for this, which had many flaws. Some programmers have been writing their own smart pointer classes or using these from Boost library - like scoped_ptr and shared_ptr.

The new C++11 standard (called C++0x before release) defines new smart pointers, similar to these from Boost. They are called unique_ptr and shared_ptr, they exist in std namespace and require #include <memory>. Microsoft Visual Studio 2010 / Visual C++ Express 2010 already implement parts of this new standard. Language features like r-value reference and move semantics make these smart pointers more powerful than before.

shared_ptr is for shared ownership and uses reference counting, so it's not needed very often in my opinion. More often we are dealing with a situation where there is one, clearly stated owner of a dynamically allocated object, like a local variable in some scope or a class member. So let's take a look at how unique_ptr can be used for this:

std::unique_ptr<MyClass> ptr1(new MyClass());
// ptr1 will automatically call destructor and free the object when going out of scope.

std::unique_ptr<MyClass> ptr2; // ptr2 is NULL
ptr2.reset(new MyClass(1)); // Object is created and passed to smart pointer.
ptr2->m_Number = 2; // Object can be dereferenced like with normal pointer.
ptr2.reset(new MyClass(3)); // New object is given to the pointer. First one is destroyed.
ptr2.reset(); // Second object is destroyed. ptr2 is now NULL.

unique_ptr can be used for arrays:

std::unique_ptr<MyClass[]> arrPtr(new MyClass[6]); // Smart pointer to array.
arrPtr[2].m_Number = 10; // Indexing array like with normal pointer.
// arrPtr will free the array with delete[] when going out of scope.

unique_ptr cannot be copied, but thanks to r-value references and move semantics introduced in C++11 it can be moved, so it can also be passed as parameter and returned by value, like this:

typedef std::unique_ptr<MyClass> MyClassPtr;

MyClassPtr ProducePtr() {
    MyClassPtr ptr = MyClassPtr(new MyClass());
    ptr->m_Number = 123;
    return ptr;
}

void ConsumePtr(MyClassPtr ptr) {
    printf("The number was %d\n", ptr->m_Number);
}

ConsumePtr(ProducePtr());

MyClassPtr ptr = ProducePtr();
ptr->m_Number = 456;
ConsumePtr(std::move(ptr));

Unlike old scoped_ptr from Boost, unique_ptr from C++11 can be used inside STL containers, e.g. std::vector. Reallocation that happens inside vector will not corrupt it.

std::vector<MyClassPtr> vec;
vec.push_back(MyClassPtr(new MyClass(1)));
vec.emplace_back(new MyClass(2)); // A new, better way of adding elements.

for(auto it = std::begin(vec); it != std::end(vec); ++it)
    printf("%d\n", (*it)->m_Number);

And now the most interesting part - custom deleters! unique_ptr can be used to store any resources because you can provide it with your own code that will be used to free that resource. For example, you can print something to console before deleting object :) Deleter can be a functor passed as second template parameter:

struct MyDeleter {
    void operator()(int* ptr) const {
        printf("Deleting int!\n");
        delete ptr;
    }
};

std::unique_ptr<int, MyDeleter> ptr1(new int(1));

Deleter can also hold some state. This way you can associate additional information with the pointer, like a memory pool that the pointer object comes from. Now the sizeof(ptr3) will be 8 because it must hold deleter data next to the pointer.

class MyComplexDeleter {
public:
    MyComplexDeleter(int memoryPool) : m_MemoryPool(memoryPool) {
    }
    void operator()(int* ptr) const {
        printf("Deleting from memory pool %d\n", m_MemoryPool);
        delete ptr;
    }
private:
    int m_MemoryPool;
};

MyComplexDeleter deleterForPool20(20);
std::unique_ptr<int, MyComplexDeleter> ptr3(new int(3), deleterForPool20);

Deleter can also be a normal function, like fclose:

std::unique_ptr<FILE, int(*)(FILE*)> filePtr(
    fopen("Readme.txt", "rb"),
    fclose);

unique_ptr<int> will contain value of type int*. What if we want to store a resource using unique_ptr that is not a pointer but some handle or identifier, so this automatically added * is undesirable? It turns out that the type of stored value can be changed by defining "pointer" type inside deleter.

struct CloseHandleDeleter {
    typedef HANDLE pointer;
    void operator()(HANDLE handle) const { CloseHandle(handle); }
};

std::unique_ptr<HANDLE, CloseHandleDeleter> file(CreateFile(
    "Readme.txt", GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
// The first template parameter of unique_ptr seem to not have any menaing in this case.

Comments (19) | Tags: c++ visual studio | Author: Adam Sawicki | Share

22:49
Fri
10
Feb 2012

How to Make Visual Studio Debugger not Step Into STL

It is annoying when you debug your C++ code in Visual Studio, want to step into your function, but the debugger enters source code of some STL container or string. For example, in the following code you will first enter std::basic_string constructor, then std::vector operator[] and then body of MyFunction.

MyFunction(std::string("abc"), myVector[0]);

It turns out there is a way to disable stepping into certain code, using regular expressions. To do this:

  1. Run Registry Editor (regedit.exe).
  2. Navigate to the key appropriate to your version of Visual Studio or Visual C++. "VCExpress" is for free Visual C++ Express, while "VisualStudio" is for commercial Visual Studio. Number is product version. Additional "Wow6432Node" should be used when working in 64-bit Windows. For example:
    • Visual C++ 2005 Express in 32-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VCExpress\8.0\NativeDE\StepOver
    • Visual Studio 2005 Professional in 32-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\NativeDE\StepOver
    • Visual Studio 2008 Professional in 64-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\NativeDE\StepOver (for version 2008 it's 9.0 not 8.0, the article linked below is wrong!)
    • Visual Studio 2010 Professional in 64-bit Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\NativeDE\StepOver
  3. Create new String Value with any name and a value containing regular expression to match against identifiers you want to exclude, like "std\:\:.+" for all identifiers from STL namespace (including members of std::string, std::vector and so on). I assume you know the syntax of regular expressions.
  4. In Visual up to 2008, it starts working after you start new debug session (e.g. with F5). In Visual 2010, you have to restart whole IDE.

Here is the full story:

In May 2007 I asked the question on forum.warsztat.gd and written this blog entry (in Polish). Now I've also found this article: How to Not Step Into Functions using the Visual C++ Debugger, Andy Pennell's Blog and this StackOverflow question: Is there a way to automatically avoiding stepping into certain functions in Visual Studio?

From that I've learned that Visual Studio 6.0 used autoexp.dat file, while new versions use Windows registry.

Rules entered in registry can be suffixed with case-insensitive "=NoStepInto" (which is the default) or "=StepInto".

Aside from regular expression syntax you can use additional special codes: \cid (identifier), \funct (function name), \scope (class or namespace, like myns::CClass::), \anything (any string) and \oper (C++ operator).

Double backslashes you can meet on some web pages come from the REG file format, where backslash must be additionally escaped, like "\\:". If you enter regular expression straight into regedit, it should be "\:".

Comments (6) | Tags: debugger visual studio c++ | Author: Adam Sawicki | Share

Pages: 1 2 3 ... 6 >

STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2015 Adam Sawicki
Copyright © 2004-2015 Adam Sawicki