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

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: ,
no comments

C++ Sink Parameter Passing

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...
no comments

Make Move Constructors No-Throw

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: ,
no comments

A Motivating Example of WinDbg Scripting for .NET Developers

August 5, 2014

WinDbg scripting gets a pretty bad name -- its somewhat contrived syntax, weird limitations, and hard to decipher expressions being the common culprits. In some cases, however, WinDbg scripts can be a very effective and reliable tool for extracting information from memory and processing it in a meaningful way. This post offers a simple example that hopefully will be useful as you begin to explore WinDbg scripts. For a more thorough explanation and more complex scripts, make sure to check my past posts on traversing std::vector and std::map. Let's set the stage with a simple console application that creates a number...

Tracking Unusable Virtual Memory in VMMap

July 22, 2014

VMMap is a great Sysinternals tool that can visualize the virtual memory of a specific process and help understand what memory is being used for. It has specific reports for thread stacks, images, Win32 heaps, and GC heaps. Occasionally, VMMap will report unusable virtual memory, which is not the same as free memory. Here's an example of a VMMap report for a 32-bit process (which has a total of 2GB virtual memory): Where is this "unusable" memory coming from, and why can't it be used? The Windows virtual memory manager has a 64KB allocation granularity. When you allocate memory directly...
one comment

Materials From My SDP 2014 Sessions and Workshops

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

Why Swift?

June 9, 2014

From the moment Apple has announced Swift, the new iOS and OS X programming language, the web is full of hate and praise, constructive criticism and pointless rants, confusion and excitement -- and many of these boil down to "why Swift?" -- namely, why Apple chose to design a new programming language rather than pick and adapt an existing one. Needless to say, I don't work for Apple, so all I can offer is an educated guess based on a lot of playing with Swift and trying to understand the mindset that led to its design and implementation. If you have...

Intents, Contracts, and App Extensions: App Communication on Android, Windows Phone, and iOS

June 3, 2014

Apple has just announced at WWDC that iOS 8 (and OS X Yosemite) will be equipped with app-to-app communication capabilities that can extend system functionality through a set of well-defined extension points. This is, without doubt, the major iOS 8 feature from my perspective, with the rest of the developer- and consumer-related features fading to the background. (Well, there's also Swift, a new programming language for iOS and OS X, to learn.) Pre-release documentation for app extensions is available here, and shows that there are several new extension points where apps can now integrate. Prior to iOS 8, the only...
no comments

iOS File Association, Preview, and Open In… with Xamarin

May 29, 2014

Many mobile apps need the ability to preview files -- email attachments, web links, cloud photos, and other assets. Some apps even need the ability to open and handle files themselves. Although file sharing between iOS applications hasn't always been available and easy, basic file sharing scenarios are now entirely accessible and easily available to any iOS app. In this post we'll take a look at how iOS apps can register as a file type handler for a specific file type, how apps can preview files, and how apps can trigger an "Open in..." dialog so that another app...
tags: , ,
one comment

Identifying Specific Reference Type Arrays with SOS

May 1, 2014

When you're looking for arrays of a specific type using SOS, you might notice a weird phenomenon. Value type arrays (such as System.Int32) will be shown properly regardless of which command you use, but reference type arrays (such as System.String) exhibit some weird behavior. Here's an example: 0:000> !dumpheap -stat Statistics: MT Count TotalSize Class Name ... 00007ffecf435740 2 304 System.Byte 00007ffecf4301c8 2 320 System.Threading.ThreadAbortException 00007ffecf4327d8 11 ...
no comments