|
An ordinary VB developer shares his own successes and failures |
|
| Home News Articles Resources Tips Downloads About me | ||
Beware of the initialization orderAn elementary initialization bug explained.Recently, I've discovered an interesting bug in my CCU messaging softwareThe 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
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 ClassNow 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 | ||