Diagnosing Native Memory Leaks with ETW and WPA

Tuesday, December 2, 2014

As a followup to my previous post on native memory leaks, here's a quick walkthrough for diagnosing memory leaks using Event Tracing for Windows. The process is fairly simple. The Windows heap manager is instrumented with ETW traces for each memory allocation and deallocation. If you capture those over a period of time (when your application is leaking memory), you can get a nice report of which blocks were allocated during the trace period and haven't been freed. If you also ask ETW to capture the call stack for allocation events, you can see where the application is allocating...

Native Memory Leak Diagnostics with Visual Studio 2015

Monday, December 1, 2014

The Current Landscape of Native Memory Diagnostics Leak diagnostics is a nasty business in native applications. There have been many attempts at solving this problem automatically. To name a few: The CRT Debug Heap (which is no longer used by default in Visual Studio 2015! - See update below.) can help identify memory leaks by associating each allocation with additional data on the allocating source file and line number. At program exit (or whenever a special CRT function is called), all blocks that haven't been freed are printed out. This has been around forever. The problem is that you need to...

Marshaling .NET and STL Collections in C++/CLI

Monday, August 25, 2014

When working with C++/CLI, you often have to convert C++ types to CLR types and vice versa. This most commonly happens with strings, but custom types and collections are just as painful. Visual C++ ships with a marshaling library in the msclr::interop namespace, which focuses on marshaling strings. However, it is notably lacking in the ability to marshal collections -- so you're left to your own devices if you have a std::map<std::string, std::vector<double>> that you need to mash into a .NET Dictionary<string, List<double>>. My wife and I spent a weekend writing a template library that marshals .NET and STL collections....
tags: ,
one comment

C++ Sink Parameter Passing

Thursday, August 21, 2014

C++ is the most complex language I know, and its parameter passing rules are only getting more arcane now that we have rvalue references in C++ 11. In this post I'd like to examine the specific scenario of passing parameters to a sink method, which consumes its parameters. What do I mean by "consume"? If a parameter is movable and safe to move from (i.e. it's an rvalue), it should move from it; if a parameter is only copyable, it should copy it. Here are a couple of sink methods from the C++ Standard Library: std::vector's push_back method will move from...

Make Move Constructors No-Throw

Friday, August 8, 2014

tl;dr: It's extremely important to make sure that your move constructor is no-throw and that it's marked noexcept. If you rely on the compiler-generated move constructor, you're good to go. Otherwise, read on.There is an important category of functions in C++ that are not allowed to throw exceptions. You should never allow an exception to escape from a destructor. If your destructor ends up throwing an exception, you will eventually get to a situation where your program's state is indeterminate and all you can do is exit the process. For example, if your destructor throws as part of an...
tags: ,
one comment

Materials From My SDP 2014 Sessions and Workshops

Sunday, July 6, 2014

This year's first SDP has been a huge success, with over 1,200 developers signed up for a huge variety of workshops and talks. The snow didn't keep me from getting to Tel-Aviv this time, and I enjoyed the conference atmosphere, the talks, and some great conversations. View from one of the SDP rooms. Really hard to stay focused on developer stuff :) I'm also VERY MUCH behind on emails and everything else that isn't directly related to the conference -- so it's going to take me a while to recuperate. In the meantime, here are the materials...
one comment

Wrapping Up DevWeek 2014

Sunday, April 6, 2014

I have landed from London six hours ago after a wonderful week at DevWeek. Three talks and a workshop made for a pretty busy schedule, but I still had time to enjoy London, with its unusually sunny weather. What's New in C++ 11 My first talk, at 9:30am in the morning, attracted a small audience of C++ developers. C++ 11 is a very extensive new standard, and if you read code developed in the modern C++ style, you might think it has nothing to do with your favorite language of the 1990's. Indeed, I tried to illustrate the major...

Visual C++ Compiler November 2013 CTP

Tuesday, November 19, 2013

As part of its faster release cadence, Microsoft just released a CTP of the Visual C++ compiler that incorporates more features from the official C++11 standard, the almost-official C++14 standard, and a couple of Microsoft-specific language extensions. It's available for download here and you can start using it in Visual Studio 2013 by switching the Platform Toolset to CTP_Nov2013. The VC blog covers the basics of what changed. In this post, I would like to illustrate some of these changes with concise examples. This is not an exhaustive list of every added feature; these are just the ones I...
tags: ,
no comments

Revisiting Value Types vs. Reference Types

Wednesday, April 10, 2013

Why do C#, the .NET Framework, and the CLR need value types and reference types? Why two categories of types? Why the added complexity in training developers to understand why and when to use each type of type? There are many answers, but very few get to the crux of the matter. You could try to justify the need for two types of types by looking at the semantic differences C# affords each. For example, you know that by default, instances of value types are copied when passed to a function, but instances of reference types are not -- only...

Easier Tuple-Like Classes in C#

Tuesday, February 19, 2013

A discussion during the MVP Summit prompted me to think about what would make it easier to use tuple-like classes while preserving valuable naming information of the tuple's constituents. The purpose of this exercise is to try and come up with a solution that does not require modification of existing C# syntax. To set the scene, consider the following method: bool ParseRequest(string request, out string operation, out int id) { string parts; if (request == null || (parts = request.Split(' ')).Length != 2) { return false; } operation = parts; return int.TryParse(parts, out id); } Using this method entails the very inconvenient syntax typical for out parameters: string request =...
tags: ,