Thomas Alva Edison's light bulb VBInfoZine Home
   An ordinary VB developer shares his own successes and failures
   FREE to registered subscribers.  

Beware of the initialization order

An elementary initialization bug explained.

Recently, I've discovered an interesting bug in my CCU messaging software

The serial protocol specification for the software states that when sending messages, the sending side should wait two seconds for an ACK message from the receiving side. If no ACK message is received within two seconds, the sending side should try to re-send the message two more times before giving up and throwing timeout exception.

The same holds true for the receiving side, of course.

When I was "stress testing" the software (by unplugging the serial cable :-)), it gave up after two seconds (instead of six as it should) with "send timeout" exception.

The reason for this turned out to be a very elementary initialization bug (I shame myself :-().

This is how the send / receive retry count constants were initialized:

Public NotInheritable Class MessagingService
...
  Private ReadOnly _MaxSendRetryCount As Integer = _MaxReceiveRetryCount
  Private ReadOnly _MaxReceiveRetryCount As Integer = 2
...
End Class
Now your'e seeing the bug, I guess - the _MaxSendRetryCount field is declared before the _MaxReceiveRetryCount field, so it is initialized with the default value (zero). That was the reason why my sending code sent a message only once and gave up after two seconds.

This kind of interdependency is easy to spot when you have just a few members in your class. However, when the number of members grows, it is equally easy to miss the interdependencies and introduce bugs just like the mine above.

Mind that initialization order, because your VB compiler won't complain!

 ©2003-2007 Palo Mraz. All Rights Reserved.   See my 'new browser window' policy