My Development Blog

My Development experiences

Archive for May, 2012

Visual Studio – File Navigation Extension

Posted by ipwright83 on May 30, 2012

One of the most frustrating things I found when I upgraded to Visual Studio 2010 was the navigation. I work with a fairly large solution with hundreds of files, many of which I can’t locate via the solution explorer as I just can’t remember where they are. To make this harder, sometimes the file names don’t match the class names and other times the name is the compound of several words that I can’t quite remember the combination of.

Within Visual Studio 2008 I used to use Resharper, and I almost exclusively used this for the file searching capabilties. It was quick and supported camel case searching. I’d previously used other solutions but found them to be slower and not as lightweight. Once I upgraded to VS2010 however I found that the search had been disabled in the express version.

Not wanting to sacrifice the brilliant searching I decided to write my own Visual Studio addin (I found this much harder than I anticipated, hence the extension doesn’t get many updates). It does exactly what I wanted, in a performant way and is avaliable to anyone who wants it. It allows searching by name or camel case and supports wildcards, with filtering as you type and highlighting of the matching criteria, bumping the best matches to the top of the list.

Image

You can find the shortcut under the ‘Edit->Find and Replace->VS File Nav’ option and can re-bind the shortcut using Edit.VSFileNav.

Feel free to have a play and leave some feedback.

Visual Studio Gallery – VSFileNav

Posted in Uncategorized | Tagged: , , | Leave a Comment »

Memory Leak within XmlSerializer

Posted by ipwright83 on May 3, 2012

Within the product I work on we used XML based serialization for some of our classes, the original aim was to provide a way of storing settings that was human readable to make debugging and upgrades easier in the future, while avoid the deprecated SOAP serialization. Unfortunately XML serialization brings about it’s own problems, most recently of which we discovered was leaking memory.

I discovered the memory ramping via Process Explorer, if you don’t have this tool I recommend downloading it for free from the SysInternals suite.

This lead down a road to our XML serialization helper class. By commenting out parts of the code eventually the problem was isolated to a section of code along the following line:

private XmlSerializer GetSerializer(Type type, XmlRootAttribute root)
   return new XmlSerializer(type, root);
}

When you construct a new XmlSerializer it will generate a temporary assembly on the disk (not however if you generate the xml serialization assemblies when you build via SGen). Normally these assemblies are loaded up into the AppDomain and then cached, however not in the case of this specific constructor call. It never caches the assembly, instead it’s re-created every time and therefore leaks memory.

The solution is fairly simple, create your own cache, in our case we made the cache based upon the Type name, and the name of the XmlRootAttribute:

private readonly Dictionary cache = new Dictionary();

private XmlSerializer GetSerializer(Type type, XmlRootAttribute root)
{
    String key = type.AssemblyQualifiedName + "|" + root.ElementName;
    XmlSerializer result;

    if (cache.TryGetValue(key, out result))
        return result;

    result =  new XmlSerializer(type, root);
    cache.Add(key, result);
    return result;
}

Note that the code above isn’t threadsafe, you’d need some form of locking if you were to call this over multiple threads. This however solved our problem and gave us a nice flat memory profiler for de serialization.

Some useful links I discovered on my way:

Microsoft’s Tess Ferrandez – .NET Memory Leak: XmlSerializing your way to a Memory Leak
Microsoft Support – Memory usage is high when you create several XmlSerializer objects in ASP.NET
Microsoft Connect – XmlSerializer memory leak

A little later I’ll be discussing Binary serialization, why I’d like to move to it instead of XML serialization and how we can work around the issue of human readability.

Posted in Uncategorized | Tagged: , , , | Leave a Comment »

Reflector context menu in Windows Explorer

Posted by ipwright83 on May 1, 2012

I often find myself using Reflector to open up DLL’s, sometimes to double check they contain the code I think they do (match the expected build version) or othertimes just to browse other assemblies. However I always find myself jumping to my desktop to find the shortcut and dragging my DLL on. I wondered to myself why I was doing this today when I can easily just extended the Explorer shell.

Image

Unfortunately in their bid to make Windows easier to use for the majority, they make it more difficult for the power users. The ‘File Types’ section that used to be avalaiable via the Tools menu in Windows Explorer has gone. This was actually just a handy way of modifying the registry. A quick bit of googling led me to the correct set of keys to add a custom context menu.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\dllfile\shell\Reflect\command]
@=”Reflector.exe %1″

Image

Placing the above in a .reg file and running it, or adding the keys manually and setting the default value will add a ‘Reflect’ option to your .dll context menu and make things that little bit easier. Happy reflecting.

Image

[Reflect] can be modified to change the title of the context menu item.
[Reflector.exe] needs to be modified to point to the actual location of Reflector.

Posted in Uncategorized | Tagged: , | Leave a Comment »