« March 2009 | Main | May 2009 »

April 28, 2009

How to use UMDH to find native memory leaks

The Debugging Tools for Windows packages come with a tool—UMDH.exe—that makes finding memory leaks in native code really pretty easy. There’s a Microsoft Knowledgebase Article that gives an overview of how to use the tool but it’s a little out of date. (It’s also very detailed, and I usually just want a summary.)

All these steps assume you have the latest Debugging Tools package installed and in your path. (Note that you need to run the tools for the same platform (x86 vs x64) as the target executable.)

Start Collecting Data

At an Administrator command prompt, run gflags.exe to start collecting stack traces for user-mode allocations:

gflags –i Program.exe +ust

Collect Snapshots

Start Program.exe running, and collect a baseline snapshot (this can be done from a regular command prompt):

umdh –pn:Program.exe –f:Dump1.txt

Perform the action that leaks memory, and collect a second snapshot:

umdh –pn:Program.exe –f:Dump2.txt

(If “Program.exe” is not a unique process name, the “-p:” command line argument can select a process by ID.)

Compare Snapshots

umdh –d Dump1.txt Dump2.txt > Diff.txt

Open Diff.txt in your favourite text editor (that can handle large files!). The memory leaks are listed in descending order of bytes leaked; each should be followed by the complete stack trace of the allocation call. Depending on the cause, this may either pinpoint the bug, or at least show a good place to set a breakpoint for debugging.

Stop Data Collection

The most important step in the whole process is to turn off the data collection for your application (once the memory leak is fixed), or else your program will run slowly while the OS kernel logs every memory allocation:

gflags –i Program.exe -ust

Posted by Bradley Grainger at 4:05 PM | Comments (0) | TrackBack

April 14, 2009

DateTime and ISO8601

The ISO 8601 standard for representing dates and times defines a (large) number of string formats for serializing dates. One of the more common formats in use (certainly here at Logos) uses the “extended format”, specifies the full date and time (but only in whole, not fractional, seconds) and always uses UTC. A string in this format looks like “2009-04-14T16:19:58Z”.

The standard date and time format strings for .NET don’t include a pattern that uses this precise format. The round-trip date/time pattern (specified by “o”) includes fractional seconds (e.g., “2009-04-14T16:19:58.0785018Z”), whereas the universal sortable date/time pattern (specified by “u”) has a space instead of a ‘T’ between the date and the time (e.g., “2009-04-14 16:19:58Z”).

We wrote the following utility methods to parse and render DateTimes in our preferred format:

/// <summary>

/// Provides methods for manipulating dates.

/// </summary>

public static class DateTimeUtility

{

    /// <summary>

    /// Converts the specified ISO 8601 representation of a date and time

    /// to its DateTime equivalent.

    /// </summary>

    /// <param name="value">The ISO 8601 string representation to parse.</param>

    /// <returns>The DateTime equivalent.</returns>

    public static DateTime ParseIso8601(string value)

    {

        return DateTime.ParseExact(value,

            Iso8601Format, CultureInfo.InvariantCulture,

            DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);

    }

 

    /// <summary>

    /// Formats the date in the standard ISO 8601 format.

    /// </summary>

    /// <param name="value">The date to format.</param>

    /// <returns>The formatted date.</returns>

    public static string ToIso8601(this DateTime value)

    {

        return value.ToUniversalTime().ToString(Iso8601Format, CultureInfo.InvariantCulture);

    }

 

    /// <summary>

    /// The ISO 8601 format string.

    /// </summary>

    public const string Iso8601Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";

}

Posted by Bradley Grainger at 9:24 AM | Comments (2) | TrackBack

April 8, 2009

Creating Mixins with T4 in Visual Studio

One of the (few) features I really miss from C++ is the ability to use multiple inheritance to create mixins, particularly with the Curiously Recurring Template Pattern.

Scott Hanselman blogged about T4 (Text Template Transformation Toolkit) a few months back, but only recently it hit me that T4 could be combined with partial classes to create a poor man's mixin system in C# 3.0.

For a simple example, let's consider a class that supports IEquatable<T>. The logic that’s specific to computing equality for this class will be in the Equals and GetHashCode methods:

public class TestObject : IEquatable<TestObject>

{

    public TestObject(int value)

    {

        Value = value;

    }

 

    public bool Equals(TestObject other)

    {

        return other != null && Value == other.Value;

    }

 

    public override int GetHashCode()

    {

        return Value;

    }

 

    public int Value { get; set; }

}

To round out this class, we really should implement Equals(object) and overload the equality operators. This is exactly where a mixin would be useful, because that code is exactly the same in every implementation of an equatable object.

Firstly, prepare TestObject to support mixins by making it a partial class:

public partial class TestObject : IEquatable<TestObject>

Secondly, create the mixin source: the templated methods of the equatable implementations. Add a new item to the project, and set the name to “EquatableClass.tt”. (This implementation will be for classes only; a separate mixin would need to be created for structs since they can’t ever be null.)

Open the Properties window for this new file, and clear the Custom Tool property. This prevents an output file being generated for this template.

Fill in the EquatableClass.tt file with this code, a templated version of standard equality methods.

namespace <#= NamespaceName #>

{

    partial class <#= ClassName #>

    {

        public override bool Equals(object other)

        {

            return Equals(other as <#= ClassName #>);

        }

 

        public static bool operator==(<#= ClassName #> left, <#= ClassName #> right)

        {

            if (ReferenceEquals(left, right))

                return true;

            else if (ReferenceEquals(left, null) || ReferenceEquals(right, null))

                return false;

            else

                return left.Equals(right);

        }

 

        public static bool operator!=(<#= ClassName #> left, <#= ClassName #> right)

        {

            if (ReferenceEquals(left, right))

                return false;

            else if (ReferenceEquals(left, null) || ReferenceEquals(right, null))

                return true;

            else

                return !left.Equals(right);

        }

    }

}

<#+

    public void SetClassName(string namespaceName, string className)

    {

        NamespaceName = namespaceName;

        ClassName = className;

    }

 

    string NamespaceName;

    string ClassName;

#>

This template (when included in another template) outputs the standard equality methods and also provides a way for the including template to customize the output.

Lastly, write the including template. Add another file to the project, this time named “TestObjectEquatable.tt”. Paste the following code into that file:

<#@template language="C#"#>

<#@output extension="g.cs" #>

<# SetClassName("Mixins", "TestObject"); #>

<#@include file="EquatableClass.tt"#>

These four lines have the following effects:

  1. Instructs the T4 engine that code in this template is written in C#.
  2. Sets the extension of the file to “.g.cs”; this clearly identifies the output as generated code.
  3. Calls the SetClassName method defined in the first template so that the right namespace and class name are used in the generated code.
  4. Includes the first template, causing all the code it defines to be generated.

When the project is built, the TestObjectEquatable.tt template will be processed to generate a partial class containing the supporting equality methods. The C# compiler will compile this with the primary partial class, giving a complete implementation of the standard equality methods.

Now that the template is defined, only the four lines in the second template need to be copied to add equatable methods to a new class, instead of the 25 lines of boilerplate code that the template generates.

One significant drawback with this approach is that a template is only reprocessed when it changes; the template engine isn't aware that TestObjectEquatable.tt depends on EquatableClass.tt, and that it should be reprocessed if the latter changes. For this reason, it’s probably best to keep the included template rather simple; it would be better to have the generated methods delegate to composed objects or static methods on utility classes rather than including a lot of complicated logic in the template itself.

To learn more about T4, I highly recommend Oleg Sych’s blog, which contains many tutorials, examples, and information about his T4 Toolbox project.

Posted by Bradley Grainger at 8:51 AM | Comments (3) | TrackBack