TStream.Read doesn't work on array components. lazarus / 12-Debugger ---------------------------------------------------------------------- masonwheeler · 2008-01-10 21:50 I'm using an object to read BER compressed integers from a file.  (I'm aware that this implementation is a very kludgy way to do it.  It's legacy code I wrote several years ago before I became aware of a better way to do so, and I've just never gotten around to improving it.)  Here's a partial class declaration and the read procedure: Code: [Select]    TBer = class (TObject)    private       value: array[1..10] of byte;       len: byte;    public       procedure read (theFile: TStream);       ...    end; procedure TBer.read(theFile: TStream); var i, dummy: byte; begin    i := 0;    repeat    begin       i := i + 1;       theFile.Read(value[i], 1)    end [b]   until value[i] < $80;[/b]    len := i;    for i := 1 to len div 2 do    begin       dummy := value[len + 1 - i];       value[len + 1 - i] := value[i];       value[i] := dummy    end end; This procedure works just fine on Delphi, but Lazarus doesn't seem to like it.  The line I wrote in bold evaluates to TRUE every time it runs. I don't have the same problem using read() with non-array variables in other parts of the program, which makes me think that either TStream.read (or TFileStream.read, as the stream in question is a TFileStream) is passing corrupt data to the array element, or that the element's memory address is being passed incorrectly. Unfortunately, the problem is compounded by the debugger's inability to look inside of objects and view their data members, so I can't inspect self and see what's going on. Any help here? ---------------------------------------------------------------------- Phil · 2008-01-13 04:22 Quote from: "masonwheeler"This procedure works just fine on Delphi, but Lazarus doesn't seem to like it.  The line I wrote in bold evaluates to TRUE every time it runs. I don't have the same problem using read() with non-array variables in other parts of the program, which makes me think that either TStream.read (or TFileStream.read, as the stream in question is a TFileStream) is passing corrupt data to the array element, or that the element's memory address is being passed incorrectly. So it reads a stream of bytes until it finds a byte with the high bit set? And you're saying that you're hitting the high-bit byte before you think you should be hitting it? Maybe a simple test app that uses TFileStream would be the place to start to determine if the problem is there. TStream and TFileStream are used pretty extensively so it would be kind of surprising that they're still buggy, but you never know. Thanks. -Phil ---------------------------------------------------------------------- masonwheeler · 2008-01-13 16:44 I'm not hitting the high-bit byte too early.  I wrote the app in Delphi, where it runs smoothly.  (That particular piece of code dates back about 4 years, and has never given me any trouble until now.)  I've run it under both Delphi and Lazarus, parsing the same data file.  This routine gets called about 1/4 of the way in.  I've verified that the Lazarus version is at the same position in the file that the Delphi version is at, at this point.  Then it tries to read something directly to an array element and we begin to have divergent behavior. I'm somewhat inclined to agree with you--the problem's most likely not in TStream.  More likely, it's got something to do with passing an array element to a var parameter.  I suspect that the wrong memory address is getting passed, producing corrupt data.  But I can't do too much to verify this, as the debugger doesn't show memory addresses.  Can anyone check up on it? ---------------------------------------------------------------------- Phil · 2008-01-13 18:04 Quote from: "masonwheeler"I'm somewhat inclined to agree with you--the problem's most likely not in TStream.  More likely, it's got something to do with passing an array element to a var parameter.  I suspect that the wrong memory address is getting passed, producing corrupt data.  But I can't do too much to verify this, as the debugger doesn't show memory addresses.  Can anyone check up on it? Maybe try passing in a non-array variable, either local or global, and see if this makes any difference. Thanks. -Phil ---------------------------------------------------------------------- masonwheeler · 2008-01-13 18:06 It works just fine.  As I said, this is about 1/4 of the way through the file and everything's gone fine up to this point.  Only when I begin to pass array elements in does it start to cause trouble. ---------------------------------------------------------------------- Phil · 2008-01-13 19:32 Quote from: "masonwheeler"It works just fine.  As I said, this is about 1/4 of the way through the file and everything's gone fine up to this point.  Only when I begin to pass array elements in does it start to cause trouble. Sounds like you've found a workaround? To get this fixed, create a small example program and post it on the FPC bug tracker. Remember, it has to be something people can compile, not just look at. Thanks. -Phil