When I originally released OCInject I omitted one important feature, lifestyle management.  This coupled with the release of a feature full TinyIOC has made me re-evaluate my position on not adding too many features to OCInject.  Release 2 of OCinject brings the following features:

  • Life style management – It’s possible to register types as singletons or instance
  • Func<T> factories – OCInject from day one supported delegate factories, now you can use Func<TContract> instead of typed delegates
  • Simplified Registrations In the previous release of OCInject types are registered using TContract –> TImplemenation to enforce programming to interface.  It’s now possible to register with TContract as the concrete type with one call
  • Largest Resolvable ConstructorIn the previous release of OCInject it simply grabbed the first constructor it found.  It will now select the greediest constructor it can resolve.  It makes the assumption that any known types are resolvable, for performance reasons.
  • Unresolveable CallbackIt is now possible to supply a call back function if the container can’t resolve a type.
  • Child Container SupportOCInject can create child containers that call back to the parent for any unknown types. 

The latest stable version can be downloaded from Codeplex and all stableish development releases can be found at BitBucket.

Future Features

One major feature still missing from OCInject is named registrations.  This is because I personally dislike ‘magic strings’, with this in mind a planned future feature of OCInject is factory delegate registration only.  Also, auto generated factories from interfaces.  More to come on this in a future post.

Life Style Management

By default all types registered with the OCInject container are transient. You can register a type as singleton in two ways.  The first method is to use .AsSingleton() this will cause the object to be created the first time it is requested.  The second method is to use .AlwaysReturnObject(obj), this will return the instance you specified.   When using either method, if the type implements IDisposable it will be disposed when the container is.  Usage example:

ClassFactory container = new ClassFactory();

//Normal Singleton
container.RegisterType<TestClass>()
           .AsSingleton();

//Preconstructed Singleton
AnotherClass instanceOfAClass = new AnotherClass();

container.RegisterType<TestClass>()
         .AlwaysReturnObject(instanceOfAClass);

Func<T> Factories

When resolving constructors if OCInject discovers a Func<T> where T is a registered type, it will pass in a func to create the type.  This uses the standard container resolve, so if T is a singleton you will always get the same instance when the factory is called. Usage example:

class FuncConsumer
{
	public FuncConsumer(Func<TestClass> factory)
	{
	}
}

ClassFactory container = new ClassFactory();

container.RegisterType<FuncConsumer>();
container.RegisterType<TestClass>();

//Successfully created with the ability to create TestClass
FuncConsumer f = container.ManufactureType<FuncConsumer>();

Simplified Registrations

Registrations no longer require the separation of contract and implement so just an implementation can be registered.  Usage example:

ClassFactory container = new ClassFactory();

container.RegisterType<TestClass>();

var t = container.ManufactureType<TestClass>();

Largest Resolvable Constructor

This is quite a complicated area that is worthy of a blog post itself but OCInject’s behaviour has changed.  When creating a type the constructors are ordered so the largest, in terms of parameters, is first.  It then looks at each parameter and to see if it can resolve it, first by checking ‘resolve time args’ (values passed in when the resolve is requested, normally from generated factories) then by seeing if the type is a registered contract within the container.   It does not check if the type can be created just that it knows about it, if it’s registered it assumes it can be created.

The first completely resolvable constructor will be used to construct the type.

Unresolveable Callback

If the container is unable to resolve the type you can now register a function that is called before the container throws an exception.  To do this you need register a Func<Object, Type> with the CallToResolve propriety.  Returning null will cause the container to throw an exception.  Usage example:

ClassFactory container = new ClassFactory();

container.CallToResolve = (type) => { return new TestClass(); };

ITestClass manufacturedType = factory.ManufactureType<ITestClass>();

Child Container Support

Calling CreateChildContainer() will return a new ClassFactory object with no registrations but a link back to the parent.  If a type is not known to the child it will ask the parent to fulfil the request.  Any registrations with the child will not effect the parent and singletons registered with the child will be disposed when it is. Usage example:

ClassFactory container = new ClassFactory();
ClassFactory child = factory.CreateChildContainer();
, ,

A little while ago I wrote a quick start guide to branching in Mercurial and, as is normally the case when you don’t actively follow development, Mercurial 1.5 has been released with a lovely new feature which impacts working with branches. 

My biggest annoyance when working with branches is by default all branches are pushed/pulled to the remote repository.  When in reality, I often want to just push the branch I’m currently on.  Mercurial addressed this by adding a branch only option to Push, Pull, Clone, Incoming and Outgoing.  To use the branch only option you append –b to the command followed by the branch name, for example:

hg push -b default

This will only push the default branch back to where the repository was cloned from (You can still specify a location to push to if required.)  This works exactly the same for all the other commands.

A handy shortcut to push the current branch is use ‘.’ as the branch name:

-b .

This will perform the command only to the branch you’re currently on.  Since I tend to push more than I do most of the others, I have set up an alias to map push current branch to pc.  You can do this by adding a section to your hgrc like:

[alias]
pc = push -b .

Branching Without Having It In Your History

Cloning locally has always been an option to create a branch or fork of the code that is still linked to the original code base, IE you can push and pull to it, but it doesn’t become part of your history in the other repo unless you pushed it back.  But I’ve never liked it because it takes a copy of everything and sets the current branch to the default branch.  Trivial, maybe, but not something I found desirable.

The new branch options makes cloning locally a lot more attractive to me.  It means I can just clone a branch, make a quick change or two and either merge it back in or delete the directory and pretend I never had that idea!

Powershell and Mercurial

As part of my continuous improvement I’ve been learning PowerShell.  One way I’ve done this is by replacing all my cmd.exe usage with the PowerShell prompt instead.  This led me to discover an excellent cmd-let (script or whatever the proper name is, I’m still learning :) ) to display the name of the current branch and status of it when you’re in a Mercurial repository.  It looks like:

Powershell Status Display More information can be found about this here.

, , ,

In my last post I talked about passing reference types using the ref keyword but it didn’t make a lot of sense.  So I just want to go over it again, hopefully making a bit more sense.

When a method is called in C# a copy of the all parameters are given to the method.  This is fairly obvious with value types because if we change the value of an int, for example, the caller does not get the updated value.

However this is not so clear for reference types.  The called method can update the state of the object it was passed, for example append extra data to a StringBuilder, and the caller’s object will have these updates.  This can lead to confusion about what is really happening, it looks as if the StringBuilder was passed by reference but a copy of the reference to it was taken.  

It maybe subtle semantics under normal use but it becomes key to understanding behaviour when the ref keyword is used.  For value types this means that if we increment an int we are passed, the caller will have the new value too.  For reference types the reference we are passed is actually a reference to the caller’s reference. Meaning if we assign a new reference to it, the caller will get the new reference.

We can demonstrate this with the following code: 

static void Main(string[] args)
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Added by Main");

    AddToSBPassedAsNormal(sb);
    Console.Write(sb.ToString());
    //Output: Added by Main
    //        AddToSBPassedAsNormal

    AddToSBPassedByRef(ref sb);
    Console.Write(sb.ToString());
    //Output: AddToSBPassedByRef

    AddToSBPassedAsNormalNewUsed(sb);
    Console.Write(sb.ToString());
    //Output: AddToSBPassedByRef

    Console.ReadLine();
}

private static void AddToSBPassedAsNormal(StringBuilder sb)
{
    sb.AppendLine("AddToSBPassedAsNormal");
}

private static void AddToSBPassedByRef(ref StringBuilder sb)
{
    sb = new StringBuilder();
    sb.AppendLine("AddToSBPassedByRef");
}

private static void AddToSBPassedAsNormalNewUsed(StringBuilder sb)
{
    sb = new StringBuilder();
    sb.AppendLine("AddToSBPassedAsNormalNewUsed");
}

From the code above we can see that the StringBuilder after the first method contains both strings.  But after the method call, where it is passed by ref, the previously entered data has been lost and, finally, using new when not being passing by reference has no effect on Main()’s reference to the StringBuilder.

Before passing a reference type using the ref keyword you must think carefully about the implications of the caller changing the reference.  As it can lead to some esoteric and difficult to track down bugs.

, ,

For the past couple of weeks I’ve been deep in some legacy code.  The code has all kind of hidden charms, while I’m not going to be overly critical because it was written at a time when a .NET 2.0 application was cutting edge.  I uncovered this gem:

public void SomeHighLevelFunction(out String feedback)
{
	StringBuilder mySb = new StringBuilder();

	_WorkerItem1.DoWorkOne(ref mySb);
	_WorkerItem2.DoWorkTwo(ref mySb);
	_WorkerItem3.DoWorkThree(ref mySb);

	feedback = mySb.ToString();
}

Ignoring the void with the out String, why pass the StringBuilder by ref?  This code was written by a migrating C++ programmer; if you’ve worked with C++ at all a little light bulb may have just gone off for you.  It works, even though it might be the very definition of programming by coincidence, so why is this so bad? 

Why So Bad?

When calling a method in C# all parameters are passed by value.  It is a common misconception that reference types are passed by reference when they are infact passed by value.  This backs up the position of the C++ programmer, so what is the difference?

C# and its documentation has no concept of pointers (outside of IntPtr) when they are used extensively under the hood.  When you call a method and pass a reference type you are actually passing a pointer (that lives on the stack) to a memory location on the heap.    The pointer is copied so you can still manipulate the memory but you can’t affect the reference.  For example:

static void Main(string[] args)
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("Added by Main");

    AddToSBPassedByRef(ref sb);
    AddToSBPassedAsNormal(sb);

    Console.Write(sb.ToString());

    Console.ReadLine();
}

private static void AddToSBPassedByRef(ref StringBuilder sb)
{
    sb = new StringBuilder();
    sb.AppendLine("Added by AddToSBPassedByRef");
}

private static void AddToSBPassedAsNormal(StringBuilder sb)
{
    sb = new StringBuilder();
    sb.AppendLine("AddToSBPassedAsNormal");
}

 

Has the output of:

Added by AddToSBPassedByRef

This could be confusing to a C++ developer because all classes, in C++, are created on the stack unless you declare a pointer and use new to create them on the heap.  Having said that a simple rule applies to both to C# and C++, if you have to use new it gets created on the heap and everything else is created on the stack.  Anything on the stack, value types or reference types, is copied between method calls.

</rant>

,

I’m currently in my third week of pair programming and I wanted to talk about some of my experiences, both positive and negative.  I pushed for a pair programming ‘experiment’ at work but I was dubious as to its value and usefulness in the cut and thrust world of professional software development.  So the justification for it seems a good place to start.

Two Expensive Resources Working Slower, Yeah Right!

Let’s not beat about the bush, pair programming means that two expensive resources, developers, are tied up on a task without necessarily bringing two ‘man days’ to a task.  What pair programming aims to do is reduce the overall cost of ownership of code.  It can work as a mentoring technique to develop less experienced programmers but this maybe defeating the object of the type of pair programming that really brings value.

In a traditional software development lifecycle the cost of fixing a bug rises exponentially as you move through the software’s (or feature’s) lifecycle.  I’m sure we’ve all seen a graph similar to below at some point in our careers.  It shows the costs of fixing a bug versus the stage development is currently in.

costCurve3

The aim of pair programming is to raise the costs slightly at the start, with a view to reducing the sharp curve towards the end.  So we end with a smoother curve and we spend list time in the costly area of the graph.

The Good, the Bad and the Ugly

My experience has been overwhelmingly positive, but the ones that stick out the most are:

  • More Focused – I’ll admit I have problems concentrating at times, I’ll see a tweet, Google something related to what I’m doing and find an interesting blog, etc.  Working in a pair has really focused me on the task at hand and I think I’m more productive.
  • Less Rabbit Holing – I’ve found just verbalising my ideas about what I want to do with a design, refactoring or while investigating a bug helps to work out problems or see things I’ve missed.  Also, the navigator can ask questions about less clear areas.  All leading to a better design, bug fix, etc.
  • Differing Styles – Participating in or watching someone else’s thought process has helped show me areas where I could improve and given me new approaches to try when blocked.
  • Naming – One thing I struggle with at times is how best to name things to best express my intentions, having a partner to ask “this is what this does, do you think that’s descriptive?” .  All this leads to better and more maintainable code.

The not so good:

  • Breaks – It can be awkward to be stuck to someone else’s brew up times, smoke breaks, lunches, etc.  It can break the flow and make it feel like you’re spending time waiting on others.
  • Thinking Time – This is very subjective and probably should be a pair activity, but sometimes when confronted with difficult to understand legacy code, a strange bug or just several choices.  It’s nice to go through your ritual, whether that be listen to your favourite music, rock the water cooler or chat to that cute girl in marketing, it all has the same outcome you feel better and have had thinking time.  I’ve found this particular hard when your partner is looking, listening, questioning and waiting for you to carry on.

Overall, it’s definitely been a worthwhile experience and one that I will be continuing.

Remember It’s Not For Everyone

We’ve been careful to explain to people that it is not an experiment with a view to introducing it across the team, just something we wanted to do.  It started off with just two of us but a third has joined with one or two more interested in joining if the opportunity came up.  But it’s important you don’t force the issue.  For it to be a success you need people who:

  • Check their ego at the door – There is no room for “this is the way I do it and it’s the right way” one of the biggest take aways for me has been learning from someone else’s style and approach, even if I disagreed.
  • Are Interested – This is so important, for the navigator role to work well the person needs to want to do it.  Not just be sat thinking “God, when can I drive”.
  • Be Strong – I have a strong personality and it can over power people around me.  As much as I’ve tried to rein it in when working with less strong personalities, it’s still important the other person is strong and is willing to defend their opinion.
,

I’m a recent convert to the ways of the DVCS and my chosen flavour is Mercurial.  There is tons of documentation out there but I couldn’t find a “Quick Start for Half Wits to Branching” that appealed to my simple nature.  So I thought I’d have a go at writing one based around my current workflow.

Mercurial has several ways to work with branches but I find named branches best suit my feature branch approach to using SCM.  There is an excellent guide to all the types with their advantages and disadvantages, here.

I’m going to assume you’re familiar with basic push, pull, and commits.

Creating and Moving Between Named Branches

To create a new named branch you simply enter:

hg branch "<branch name>"

This will mark the current working revision with the branch name you specified but it will not be part of your repository till you next do a commit.  When committing it will create the branch and add all the changes since your last commit.  This is quite a big gotcha, if you want to any changes to be committed on the old branch before moving to the new one you must do a commit before branching.

Finding out the name of the branch you are on is be done by entering:

hg branch

You can list all of the branches that are currently open by using:

hg branches

This outputs a list of branches with the changeset number that is the head of the branch, for example:

hgbranches2The ‘default’  branch is setup when the repository is first created and is the equivalent of a mainline or trunk branch.

To move between the branches you do an update with the name of the branch in.  This will fail if you have any uncommitted changes but you can force it to update and lose any the changes by using –C.

This will just move between branches:

hg up "<branch name>"

This moves between branches losing any changes:

hg up -C "<branch name>"

The following examples ties a few of these together and shows how creating a branch without committing first moves any recent changes into the new branch.

sample1

Merging

Merging two branches is done by updating to the branch you want to be the target then merge in the changes from the source branch by specifying its name. For example if you wanted to merge the latest edits from default into the feature branch named “Feature X” you would do:

hg up "Feature X"
hg merge “default” 

 

It will attempt to merge the changes without any user intervention but if it can’t it will ask the user to do it.  On Windows, KDiff3 is installed along with Mercuiral and is used to merge changes.  Again none of these changes will be saved until you commit.  If you regularly work on a feature branch while others update the default branch, I would strongly urge you regularly merge updates from the default branch to stop you having one almighty nightmare merge at the end.

Merging offers a preview option by passing –p to the merge command.  This allows you to see upfront if there will be any conflicts or problems, I find this quite useful when assessing how long a merge will take. 

Pushing 

Mercurial by default pushes your whole repository, including all your local branches, to the remote repository.  It works exactly the same if the branch (or branches) exists in the remote repository.  If they don’t it will fail and you can use the –f option when pushing to force it to create the branches on the remote repository.

But sometimes you want to just push changes on the current branch, leaving the others behind.  I’ve recently discovered a great tip for doing this, you can just push the current revision with its associated history and parent by using:

hg  push --rev .

The ‘.’ is a shortcut for the most recent changeset on the branch and it will only push changes on that branch.  This, of course, means that the branch must exist or be created on the parent.

Closing

Mercurial doesn’t support the deleting of branches (at least that I’ve come across) but you can close a branch.  Closing a branch is done by selecting the branch and doing a commit with the close flag, for example:

hg up "<branch to close>"
hg commit --close-branch

This marks the branch as inactive and stops it appearing when you list the branches using hg branches.  It is possible to still see all branches including closed ones using the this command

hg branches -c

One major downside is, closing a branch does not prevent it from being pushed.  This means you will still have to create it on the server or just push the current changeset as shown above.

, ,

**Update** I’ve created a CodePlex project: http://ocinject.codeplex.com

I’ve been learning about Dependency Injection (DI) and Inversion of Control, one of the ways I’ve done this is by creating my own mini DI framework for use in DirLinker.  I’ve, also, looked at the big name frameworks and read lots along the way but I want to give something back to the community that has taught me so much.  So I’m releasing my tiny DI framework used in DirLinker independently as OCInject.

OK, So Why Is This Different To X

My aim when creating OCInject was to create something that can be used in small projects, single exe utilities or one off apps, where you want the advantages of a DI container but without the overhead of Castle, Autofac, et al.  So consider this DI lite, you simply take the two files, IClassFactory.cs and ClassFactory.cs, and drop them in to your project.  That’s it, done, no external dependency and no XML config file. What do you get?

Features:

  • A DI Container with fluent like configuration
  • Ability to resolve constructor parameters for registered types and passed in constructor arguments
  • Runtime delegate factory generation
  • Pseudo session support via IDisposable.

The feature list is tiny and is very unlikely to grow; well maybe by one, singleton support.  It is not meant to compete with or to replace anything that already exists.

How To Use It

Basic Resolution

As I said above first thing to do is copy IClassFactory.cs and ClassFactory.cs into your project, then fill the container and resolve your type.  For example

    public interface IMyClass
    {    }

    public class MyClass : IMyClass
    {  }

    public class MyApp
    {
        public void Run()
        {
            IClassFactory factory = new ClassFactory();

            factory.RegisterType<IMyClass, MyClass>();

            IMyClass myClass = factory.ManufactureType<IMyClass>();
        }
    }

If the class has dependencies we can inject these just by specifying them as constructor arguments on our class and the container will resolve them if they are registered.  For example

public interface IMyClassWithDepend
{}

public class MyClassWithDepend
{
    public MyClassWithDepend(IMyClass depend)
    { }
}

public class MyApp
{
    public void Run()
    {
        IClassFactory factory = new ClassFactory();

        factory.RegisterType<IMyClassWithDepend, MyClassWithDepend>();
        factory.RegisterType<IMyClass, MyClass>();

        IMyClassWithDepend myClass = factory.ManufactureType<IMyClassWithDepend>();
    }
}

Auto Delegate Factories

For auto generated delegate factories we need to create a delegate that returns the contract type. Then register this with the container using the WithFactory<T> method when registering the type. For example:

public delegate IMyClassCreatedByFactory FactoryMethodName();

public interface IMyClassCreatedByFactory
{ }

public class MyClassCreatedByFactory
{ }

public class FactoryConsumer : IFactoryConsumer
{
    FactoryMethodName _Factory;
    public FactoryConsumer(FactoryMethodName factory)
    { _Factory = factory; }

    public void DoWork()
    {
        IMyClassCreatedByFactory c = _Factory();
    }
}

public class MyApp
{
    public void Run()
    {
        IClassFactory factory = new ClassFactory();

        factory.RegisterType<IFactoryConsumer, FactoryConsumer>();
        factory.RegisterType<IMyClassCreatedByFactory, MyClassCreatedByFactory>()
            .WithFactory<FactoryMethodName>();

        IFactoryConsumer myClass = factory.ManufactureType<IFactoryConsumer>();
		myClass.DoWork();
    }
}

Auto Delegate Factories With Parameters

Delegate factories can take parameters and pass them on to the constructors of objects.  This is still a bit limited because it doesn’t intelligently select the correct constructor, just the first it comes across (maybe a feature for the future :P ).  To use them just add parameters to your delegate declaration and a create a constructor on your implementation type that matches.  You can still have dependencies that are resolved by the container in the constructor, for example:

public delegate IMyClassCreatedByFactory FactoryMethodName(String param1);

public interface IMyClassCreatedByFactory
{ }

public class MyClassCreatedByFactory
{
    public MyClassCreatedByFactory(IMyClass myClass, String param)
    { }
}

public class FactoryConsumer : IFactoryConsumer
{
    FactoryMethodName _Factory;
    public FactoryConsumer(FactoryMethodName factory)
    { _Factory = factory; }

    public void DoWork()
    {
        IMyClassCreatedByFactory c = _Factory("OCInject filling a gap that doesn't exist");
    }
}

public class MyApp
{
    public void Run()
    {
        IClassFactory factory = new ClassFactory();

		factory.RegisterType<IMyClass, MyClass>();
        factory.RegisterType<IFactoryConsumer, FactoryConsumer>();
        factory.RegisterType<IMyClassCreatedByFactory, MyClassCreatedByFactory>()
            .WithFactory<FactoryMethodName>();

        IFactoryConsumer myClass = factory.ManufactureType<IFactoryConsumer>();
        myClass.DoWork();
    }
}

Where to Get it From and Further Examples

You can download it from BitBucket at http://bitbucket.org/humblecoder/ocinject/.  I don’t have any documentation at the moment but you can look at the DirLinker source and the unit tests for OCInject for more examples.

Enjoy and I hope someone finds it useful :)

, , , ,

I am a fairly junior developer with just short of 3 years commercial experience in an office where the average experience level is 10+ years.  I love to read and learn about software development nearly as much as I love to actually write code and I have lots of ideas and opinions on code, style, design, etc,. For several years I’ve been trundling along nervously squeaking up to more senior staff members and often being shot down or told “that’s a great idea but we don’t have time/resource/it doesn’t fit in with what we do.”.  This has all lead to a mixture of frustration and self doubt as to if what I am learning is right or even unprofessional at times. Enter NxtGenUG Manchester.

What Is It?

NxtGenUG is a .NET users group that meets once a month.  The meetings generally consist of a short member ‘nugget’ (~10 minutes), a longer ‘featured’ speaker, eating pizza, giving away swag and most importantly, yes even more important than pizza, socialising with other interested developers. 

The nugget is a short talk from a member about something they are learning about or know about.  A wide range of topics have been covered from printing in SilverLight to the Pomodoro technique.  While ten minutes is not a very long time it does give a wonderful little introduction to a topic and provide a talking point over pizza.

The main attraction, as it were, is a full hour long presentation on a topic.  There has been some quite interesting presentation from varying quality presenters.  The highlights including a crash course in TypeMock and a tour of PLINQ.  Even if the topics are not things you work with directly, it’s a great way to learn about new things and keep up to date.

Why Has It Brought Out The Best In Me?

While all the presentations and information have been great what has really helped me is the social side of things.  I do a lot of reading and learning and I form opinions based around it.  What I really want is place to voice these opinions and get a good debate going with like minded people.  I am a true believer in “Strong opinions, weakly held” and from this point of view the conversation I’ve had while at the user group have really challenged me and changed my thinking, for the better.

Don’t get me wrong people at work are interested as well, but it’s nice to see and hear other people in the community talk about their experiences and the challenges they’ve faced introducing Agile, TDD, etc,.  This type of information sharing is invaluable and really allows you to see things from a completely different point of view, than chatting with colleagues would normally do.   For example,  I’ve gotten several invaluable tips for guerrilla tactics to introduce things in a resistant culture.

More importantly for me, it’s given me the confidence to fight my corner more in the office and really push forward.  Listening to other people talk and talking to speakers, I’ve realised that you don’t have to be right all of the time, just open minded and have confidence in your own knowledge.  I have taken this to an extreme level by presenting my own member nuggets at the user group, starting this blog and running several training sessions/OpenSpaces discussions at work.  Now I’m looking toward getting on the amateur speaking circuit.

In summary, just being around other people from different work environments and cultures to mine has really driven me and given me the confidence to push forward with change.  User groups aren’t just about the presentations, they are about the community and improving yourself and workplace through other peoples experiences.   For me this has been a massive success so thank you Steve, Andy, John, Joel and countless others.

, ,

Introduction

I recently attended the Manchester NxtGenUG “Open Mic.” Night that was run as an OpenSpace style event. I had never heard of OpenSpace before and I wasn’t to sure what to expect from the open mic night but it turned out to be an extremely enjoyable experience with lots of valuable information. Thanks to Steve and Seb for making it such a great night!

I shameless stole the format and used it as a team building exercise at work. I had several worries because of the small team and other constraints that meant it couldn’t be a true OpenSpace event.

So What Is An OpenSpace Event

An OpenSpaces conference is one with no agenda, keynote or dedicated speakers, just passionate people talking about what they are interested in. When I was looking for more material to help explain it to people at work I came across this great quote about the idea behind OpenSpace.

In my experience open space is based on the belief that we humans are intelligent, creative, adaptive, meaning- and fun-seeking. It sets the context for such creatures to come together knowing they are going to treat each other well. When this happens there is no limit to what can unfold.

Alan Stewart
personal communication

source and excellent article about OpenSpace

The format of them is fairly lack. You get all the participants to sit around in a circle then go through them one by one asking for a topic they would like to talk about. Once all the subjects are collated a vote is taken as to which to talk about. When a subject is chosen the person who chose the subject starts the conversation by expanding on it. The conversation should be flowing at this point as people will have voted for it for a reason.

Each subject is talked about till it has naturally run its course then you move on to the next. It is simply over when it’s over.

How Would This Work In The Office?

All this sounded wonderful and works great when you have like minded people and lots of time but how would this work in a workplace?  In the office we have time constraints, it has to be justifiable to the business, not everyone is as passionate about software as others and more experienced members of staff can be cynical.

The time constraint was unavoidable but I was confident even with only an hour available it could still be constructive. To make it relevant to the business I positioned it as a training session that was self led, to discover the strengths and weaknesses of the team along with exposing hidden knowledge in the team. Finally, to tackle cynical/non passionate people I spoke to as many people as possible and explained the concept and made it clear it was optional.

I ran it past the team leader and asked for permission to try it during the working day to get as many as people to come. I think I got the timing right here, we are currently in a quiet spell before the start of a new project and it is the run up to Christmas so not a lot is going on so it was given the go ahead.

So with some trepidation I arrange a meeting for late Monday afternoon.

How Did It Go?

Well in a word, fantastic. All topics chosen were topical, relevant and most importantly could be applied to the current code base and processes in the team. I was truly impressed with the level of knowledge and desire to improve that was shown.

The meeting itself got off to a bit of a shaky start with people unsure about what to expect. This is where the host (me in this case) has to really take the lead and explain to people what is going to happen. We selected a subject, which just happened to be mine. This helped as it allowed me to take the lead and show others just how easy it was. Everyone of the nine people contributed something interesting.

At the end we had a five minute retrospective (We couldn’t be Agile without it :P ), everyone was pleased with how it went but suggested next time we take more notes or have an official collator of links. So this is something we are going to try.

Conclusion

We took the concept and made it fit within it our situation, I see this as an evolving thing inside the team and each time we will tweak it to fit the topic or mood of the team. The biggest takeaway, for me, has to be that it turned into a great session not only did it show where people’s interest and knowledge lies but it was a great team bonding exercise.

I can’t recommend trying something like this in your team enough, remember:

  • Don’t stick to the format rigidly
  • Trust your team to bring up the problem areas or where they want to improve
  • Have fun!
  • Oh, and try to make sure the cake isn’t a lie.


Introduction

Lately I’ve spent a lot of time tracking down a handle leak in a .NET application that called into a DCOM object.  In the process I had a crash course in WinDBG.  Before this I was, frankly, scared of WinDBG it was just fast scrolling text with a command interface, far from user friendly.  But I have discovered it is extremely powerful and not quite the scary monster I thought it was.

To demonstrate this I’m going to use a fictional ASP.NET app called MemoryMuncher that leaks memory with each page reload to give a quick overview of diagnosing a managed memory leak with WinDBG.

.NET Doesn’t Have Memory Leaks!

It is a common misconception that .NET can not leak memory because it has a garbage collector.  The most common situation where it would leak memory is in registering delegates with an event and not unregistering them.  Consider the following code:

private void DoSomeWork()
{
    SomeClass bigClass = new SomeClass();
    HoldOnToARef.IDoSomethingImportantMaybeAjaxy += bigClass.IHandleSomethingImportant;

    //Some other code
}

When the SomeClass instance goes out of scope the pointer, bigClass, to it will be deleted.  When the GC runs next it will walk the stack looking for references to the memory and it will find one!  The one it will find is the link between HoldOnToARef.DoSomethingImportantMaybeAjaxy event and the delegate we registered with it.  But we don’t have a reference to bigClass anymore to deregister the delegate.  So it will last until HoldOnToARef goes out of scope, which it won’t in this case as it is a static.

You maybe looking thinking “I would never write code like that” but we all have (maybe not with the statics as that’s an extreme example) with objects that live for a long time or we have all inherited code like this.

Memory Muncher is…errmm….Munching Memory

Every time a page refreshes on Memory Muncher the host process’s, w3wp.exe, memory usage raises by 5MB and when left it does not go down by itself.  At this rate, on a 32bit system, the process will run out of virtual address space in 400 page views causing the app to crash and the App pool to recycle.  If it’s a 64bit system, it will just start to really slow down the server as it grows, so no big deal there :P .

The first thing to do is to take a process dump when the memory usage is high.  This is relatively straight forward on Server 2008+ / Vista+, go to task manager find the offending process, right click and select “Create Dump File” (fig1). If you’re on Server 2003 / XP you need to use the built in Dr Watson tool or Process Explorer to create a dump.

fig1: Menu option

Create Dump Menu

This should let you select a location to save the dump to or tell you where it has saved it to.

Ready, Steady, WinDBG…

Now we have the dump lets get WinDBGing.  WinDBG can be downloaded from here, you will need the 32 bit or 64 bit version depending on what you want to analyse and to analyse .NET dumps you need to do it on a machine with .NET installed.

First thing to do is open the dump and load the SOS extensions for .NET.  I normally find it best to start WinDBG elevated on Vista/2008 systems.  The SOS extension bring additional commands to WinDBG for dealing with the CLR and the managed memory model.  They are loaded by typing

.loadby sos mscorwks

This is used by .NET 2.0 + if you’re using .NET 1.x there is a different command to load the extension.

Next we need the symbols, symbols make the stack traces readable.  If this is the first time you have used WinDBG type .symfix at the prompt.  This will setup the symbols path to use the public Microsoft symbol server.  You will need the symbols for your application as well.  The best way to do this is get them together in a folder, they need to be the correct version for the build you just took the dump from. Go to the menu and select File –> Symbol File Path and then browse for the folder you put them in.  Check the reload option in that dialog then hit OK.  You should now be good to go :)

Back to Memory Muncher and we are ready to start to look for the leak.  The first thing to do is dump the heap, this done by entering the command

!dumpheap

This will give a lot of information, for Memory Muncher we ended up with: image

There is an almost overwhelming amount of information here.  What we have is, from left to right, the method table address, the number of instances of the type, the amount of memory being used in total by all instances of a type in bytes and finally the type.  I’ve highlighted the two important ones here, the number of objects for a given type and the amount of memory taken up.  The list sorted by what is taking up the most memory.

I have underlined my object that is using the most amount of memory, this seems like a good place to start.  The questions I’m trying to answer at this point is why do we have so many of these objects alive?  What is the object?  What is keeping it alive?  To start answering these we need a reference to just one of them, to get this we can use !dumpheap again but this time for the type, this is achieved by running the following command:

!dumpheap -type MemoryMuncher.SomeOtherClass

This produces:

image

Again an awful of text flying past but the important bits are the first column, this is the address of the instance of an object on the heap.  The other information is the method table address (this is for reflection to look up metadata) and the size in memory of the instance.  At the bottom is a list of who owns the references to these objects and how many of them there are.

We know that the garbage collector is compacting so that means the lower the address the older the object is, this is pertinent because if we look at the older objects they have probably survived a few garbage collector generations and as such are more likely to show us who is holding the reference causing the leak.

So, we can at this point have a look at what the object is and its current instance values by doing !do <address>.  This would help identify individual objects and what state they are in but since there is so many it would be wiser to see who is holding a reference.  This can be done by running the following command:

!gcroot <address>

This produces:

image

This output shows we are being held on to by a class called SomeClass which is being held open by an object called MyCallbackDelegate. At this point we can find these objects in the code and start the looking at what MyCallbackDelegate is.  The full Memory Muncher code is listed below and we can see that a handler is registered but never deregistered.  So it should be a simple fix.

namespace MemoryMuncher
{

    public delegate void MyCallbackDelegate();

    public class HoldOnToARef
    {
        public static event MyCallbackDelegate IDoSomethingImportantMaybeAjaxy;

        public static void DoThatImportantThing()
        {
            IDoSomethingImportantMaybeAjaxy();

        }
    }

    public class SomeClass
    {
        private List<SomeOtherClass> m_SomeList = new List<SomeOtherClass>();
        public SomeClass()
        {
            for (int i = 0; i < 500; i++)
            {
                m_SomeList.Add(new SomeOtherClass());
            }
        }

        public void IHandleSomethingImportant()
        {
            foreach (SomeOtherClass aClass in m_SomeList)
            {
                Console.WriteLine(aClass.BigByteArray);
            }
        }
    }

    public class SomeOtherClass
    {
        public Byte[] BigByteArray;

        public SomeOtherClass()
        {
            BigByteArray = new Byte[1024];
            for (int i = 0; i < 1024; i++)
            {
                BigByteArray[i] = 0xA0;
            }
        }

    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                SomeClass bigClass = new SomeClass();
                HoldOnToARef.IDoSomethingImportantMaybeAjaxy += bigClass.IHandleSomethingImportant;
            }
        }
    }
}

Final Thoughts and Further Reading

This is a fairly noddy example that would have been caught simply by the reviewing the code and as such the dump is a quick study.  In a real world example there might be multiple objects that exist in their thousands or complex object graphs that make GCRoot’s output more cryptic.  Either way the same methodology would apply, look for suspicious objects you know should have fairly short life cycle and dig into why the reference is being held open.

This is only a tiny portion of what WinDBG can do and I haven’t even began to do it justice.  If you want to know more these are great places to start

Also, a general read for all .NET developers with lots of useful background information for this is CLR via C#.

Happy WinDBGing!

,