Three bugs went into a program

| 0 Comments

Three bugs went into a program; a memory leak, a misunderstood interface and a deadlock...

The memory leak was easy to track down as it was in the code that was covered by the test harness. So we just instrument the code with Bounds Checker and run the test. A reference counted object was being mishandled and leaking. Easy fix.

The interface issue was harder. I was using a std::list<> to manage a list of items and every so often needed to walk the list and remove some entries. I was invalidating an iterator and not realising it and that was screwing things up later on in the code. The thing is, I was code blind about this one. I kept looking at the code that was in error and was just unable to see it. Eventually I just rewrote the piece of code and the problem went away and I finally realised what I'd been doing wrong :(

The deadlock was a classic resource acquisition inversion. I have two areas in the code that need locking to prevent concurrent access. In most places I was doing the locking in A then B order and in one place I was doing it in B then A. Easy to spot with the debugger as my CriticalSection wrapper class stood out nicely in the thread window and the critical sections themselves have the thread id of the owning thread easily visible in the debugger.

The first bug took under 20 mins to find and fix. The others took longer because getting to the place where the bug manifested was hard and involved running the server and multiple clients and 'doing stuff'. So the tests win, once again...

Leave a comment