Monthly Archives: October 2012

How not to re-raise an exception in Delphi

Just a quick exception management tip for today.

I was debugging a weird cascade of exceptions in an application today — it started with an EOleException from a database connection issue, and rapidly degenerated into a series of EAccessViolation and EInvalidPointer exceptions: often a good sign of Use-After-Free or Double-Free scenarios.  Problem was, I could not see any place where we could be using a object after freeing it, even in the error case.

Here’s a shortened version of the code:

procedure ConnectToDatabase;
begin
  try
    CauseADatabaseException;  // yeah... bear with me here.
  except
    on E: EDatabaseError do
    begin
      E.Message := 'Failed to connect to database; the '+ 
                   'error message was: ' + E.Message;
      raise(E);
    end;
  end;
end;

Can you spot the bug?

I must admit I read through the code quite a few times before I spotted it.  It’s not an in-your-face-look-at-me type of bug!  In fact, the line in question appears to be completely logical and plausible.

Okay, enough waffle.  The bug is in the last line of the exception handler:

      raise(E);

When we call raise(E) we are telling Delphi that here is a shiny brand new exception object that we want to raise.  After Delphi raises the exception object, the original exception object is freed, and … wait a minute, that’s the exception object we were raising!  One delicious serve of Use-After-Free or Double-Free coming right up!

We should be doing this instead:

      raise;  // don't reference E here!

Another thing to take away from this is: remember that you don’t control the lifetime of the exception object.  Don’t store references to it and expect it to survive.  If you want to maintain knowledge of an inner exception object, use Delphi’s own nested exception management, available since Delphi 2009.

When driving in a strange place doesn’t turn out quite the way you were expecting

So I flew in from Hobart, with certain expectations and negative assumptions about what driving here in Seattle would be like.  My expectations about driving here in Washington State have certainly been confounded. While I have driven here once before, it was only for a day and I was pretty jet-lagged. Here are some of my observations:

  1. Drivers are courteous; super ridiculously amazingly courteous. If, as a pedestrian, I step out onto the street, all the traffic stops for me. And not screeching to a stop and foaming at the mouth out the window, like you might expect in Australia, but just gently waiting for me to make my own foolish way across the road. Whoa.
  2. If I look like I want to cross the road, all the traffic stops for me. Yes, that’s right. And so of course I get confused by this because in Australia the complete opposite behaviour is the norm.
  3. When I ride a bike, cars approaching from behind slow down, make sure it is safe, and when the road is clear, pass me with a gentle acceleration and plenty of space. Freaky. So different to the Hobart approach of flying past mere inches from you.
  4. But stop signs completely suck. Especially four way stops. Priority is by order of arrival to the intersection. So confusing. And they are everywhere! I reckon the entire US economy could be resurrected just from the fuel savings in replacing these stop signs with roundabouts, let alone via the increase in productivity because of the savings in travel time.
  5. Right turn on red light. So you stop at the light, and if the road is clear, you can turn right even when the light is red (Aussie drivers, remember that Americans drive on the wrong side of the road). The only time you are not allowed to do this is when a sign informs you that there is “No turn on red”. But this typically only happens at intersections where visibility is lacking. Oh yeah, they hang their traffic lights up here, like Christmas decorations (but I did already know that…)

  1. But what about this traffic light?  What do you do here?  This is a trick question for Australian drivers.

    Wrong answer! It turns out that you are allowed to do a right turn on the red arrow. Yep, even though that red arrow seems to be specifically telling you otherwise. I can quote from the Washington Department of Transportation bylaws if you don’t believe me.

Of course there will be exceptions to some of the above – I did see one aggressive driver here. But only one.  And I have no idea about the situation anywhere except the Bellevue/Redmond area. But expectations about driving here completely shattered, in a good way!