@MarkMLl @VisualLab @rvk @Paolo @howardpc
Thanks for your warm hearted help! Finally I found the problem and here is the review of this stupid problem:
The program crashed at SetLength(hd,Len). @MarkMLl insist it is not the real problem. @VisualLab built a small sample to testify MarkMLl's opinion. But It always crashed at this step. I used F8 step debug, logfile and MessageDlg to confirm it really happened at this step.
But SetLength is a frequently used function and it never crashed before, even in this program. So I set breakpoints in all of the lines which used SetLength.
What was found: There is another procedure called by FormCreate. And there was something like
procedure ..........
var
arr:array of word;
fs:TFileStream;
......
begin
SetLength(arr,Len1);
fs:=TFileStream.Create(filename,fmOpenRead or fmShareDenyNone);
fs.Position:=0;
fs.Read(arr[pos],len2); //<---------------- stupid bug: pos+len2>len1. Modified, I wrote it wrongly at first time.
fs.Free;
end;
when this procedure is called by FormCreate, everything looks good. I can do anything on the Form1 window only if I don't click button1.
When button1 was clicked, this is the first time to run SetLength after the bug. Even hd is not arr the program still crashed.
So, another lesson for me: problem often happens before you can see it. Seek the real bug before the crash point.
Answer to Cyrax: I use win10 64bit and lazarus 64bit. But this program will run on a winxp 32bit system. So I installed a lazarus-cross-plateform and compile the program as a win32 exe file.
Thanks again to everybody!