Friday, December 23, 2005

Strange Bug in VB 2005 - Follow-up

OK. After some time figuring out what caused the bug, I finally discovered how to reproduce it EXACTLY.

I was reading a stream of bytes, but I was a little bit reckless -- because it was only a test not a production project -- and my reading code was something like this:
Imports System.Text

Dim aBytes() as Byte = {}

Do
Try
Redim Preserve aBytes(aBytes.Length)
aBytes(aBytes.Length - 1) = TheStream.ReadByte()
Catch ex as Exception
Exit Do
End Try
Loop

Dim strList as String = _
Encoding.UTF8.GetString(aBytes).Replace(vbLf, "").Split(vbCr)
Apparently it is in THIS case that the non-String bug occurs.

When I fix my sloppy code to something more suitable the bug didn't occur anymore. Like this:
Dim aBuffer(1024) as Byte
Dim mStream as New System.IO.MemoryStream
Dim bytesRead as Integer

Do
bytesRead = TheStream.Read(aBuffer, 0, 1024)
mStream.Write(aBuffer, 0, bytesRead)
Loop While (bytesRead <> 0)

Dim strList as String = _
Encoding.UTF8.GetString( _
mStream.ToArray()).Replace(vbLf, "").Split(vbCr)

0 Comments:

Post a Comment