Monday 29 June 2009

Shipped!

I’m pleased to announce that acceptance testing has been completed, and Baby Jack 2.0, now officially named Alaric Nahum Jack, was successfully delivered late last week.

P1040594

The last few hours of the shipping process were rather gruelling for certain members of the team – my wife was allowed to bliss out on Gas and Air (known to the trade as Entonox), but I was not permitted ear plugs!

Unfortunately, no documentation was provided, but I do have two weeks off work to try to figure out how he works!

Wednesday 17 June 2009

Magnetic Personalities

A friend of ours happened to be listening in as a primary school teacher was giving a lesson on Electricity and Magnetism.

“What M picks things up?” the teacher asked the children. Then, choosing a child from the bunch waving their hands in the air, “Yes?”

“Mum!” replied the astute scholar of human nature.

Wednesday 10 June 2009

Managed Memory leaks and their solutions

It’s a myth that managed code does not suffer from memory leaks. Managed code is as capable of springing leaks as unmanaged, though fortunately, the leaks themselves are not nearly so pernicious. In managed code, leaks are not the result of forgetting to deallocate memory – the Garbage Collector is pretty much infallible in that respect. Rather, the leaks are almost always the result of an object subscribing to an event on another object with a longer lifetime. This results in the longer-lived Grandpa object grasping so tightly to the reference to the shorter-lived object (so that it can notify it of events) that the Garbage Collector dare not clean up the whippersnapper.

Such situations can easily be detected using one of the many memory profilers around (I’ve used, and can heartily recommend JetBrains dotTrace; I’ve also heard good things about the new version of RedGate’s Ants Profiler). These help you identify the objects that you expected to be cleaned up, and then let you trace back to find the villains that are keeping them alive.

But once you’ve identified the source of the problem, what do you do? Sometimes it’s as easy as remembering to make the short-lived objects unsubscribe from the longer-lived – you can do this if the class implements IDisposable, for instance. Other times, there’s no suitable or natural point at which to do the clean-up. What then?

That’s the subject of the excellent Code-Project article Weak Events in C# which explains the problem in more detail and covers the whole range of possible solutions to the problem. Don’t let the title put you off: the solutions don’t compromise on the power of your events – you can still declare

public event EventHandler<EventArgs> CountdownToArmagedonStarted

if you want to. Rather, the title refers to the common ingredient in all the solutions, the magical WeakReference class.

Go read the article. It will be the best spent fifteen minutes of your day.

Monday 8 June 2009

Are Biology Students devolving?

As if she wasn't busy enough bringing up our daughter, and growing the new addition to the family, my wife is a part-time private tutor to several A-Level Biology students (A-Level, for those not from the UK, being the final hurdle students have to clear before University). These students must all have passed their GCSEs before being accepted on to A-Level courses, with exams including core subjects like English and Maths. But you'd never know it from the answers they give as my wife goes over practice exam questions with them.

Take percentages. One question involved converting a percentage to a frequency: if 5% of the population have the recessive form of gene X, how many is that out of 100? Blank stare.

Then there was a question about interpreting Echocardiogram (ECG) traces. You’ll have seen one these in the movies if ever there was a hospital death scene. As the patient breathes his last, the camera invariably pans to an ECG monitor at his bedside. The sound-track turns somber, the sun is blotted out by a cloud, and on the monitor the peaks and troughs of heart activity subside to the infamous flat line.

On an ECG, the trace shows activity up the vertical axis and time along the horizontal, so to calculate the heart rate (as the question required) you begin by measuring the distance between peaks on the chart to get the duration of each beat. The student could do that - counting blocks was within her skill set. But the next step was to convert that to a rate in beats per minute.

ECG Monitor: Photo Credit, Wikipedia

"It's not fair that they ask Maths questions in a Biology paper!", complained the student. My wife was taken aback for a moment – the girl apparently wasn’t joking.

"So what do you do now?", my wife prompted once she’d recovered.

"I don't know", began the student; then with sudden inspiration: "It's got something to do with 60 hasn't it?".

"Well, yes, it has,", my wife said encouragingly. "You know how long each beat lasts. Now you need to work out how many of those beats will fit in a minute. So what do you do?"

"Ah, I've got it now". The student's face lights up. My wife smiles hopefully. "You divide the time by 60!"

Thursday 4 June 2009

ASP.Net Quick Tip: Get Web Deployment projects working with Web Application Projects

Hopefully this will save someone else (or my future self) the couple of hours my colleague and I spent wrestling with ASP.Net to get this to work.

We have an ASP.Net 3.5 Web Application project and were trying to pair it up with a Web Deployment Project. This first problem we faced was with ILMerge complaining during the build process for the web deployment project. It said "An error occurred when merging assemblies: ILMerge.Merge: The target assembly 'cwSharp2' lists itself as an external reference.”. We fixed this thanks to a comment by JohnC on a post by Rick Strahl. You have to make sure that the output assembly for the Web Deployment project is different to the Web Application project – we called ours [Project].Pages.

The next problem didn’t reveal itself until we actually tried running the website as compiled by the Web Deployment project. When we tried browsing one of the pages we got errors like “Compiler Error Message: CS0433: The type '**' exists in both '***.dll’ and ‘***.dll'”. I searched, and – bing – out popped the answer: you have to right-click your web application project and click “Convert to Web Application”; doesn’t quite make sense, I know, but it works!