Range Check Error and Delphi 7.0

2019-06-21 19:05发布

问题:

After spending a week checking and fixing my program for memory leaks through FastMM4, I finally test ran my program on a different PC. Now, I am getting "Range Check Error." I have spent hours researching online about this, but none of them seem to give me what I am looking for. My program was complied with the Runtime Error option Range Check. So, I know that's why I am getting the error, but I needed to know exactly why the error is raised.

The program was compiled on XP with Delphi 7.0. The testing PC is a Windows 7. As soon as it starts up, my program begins to communicate through serial port and then followed by "Range Check Error" message boxes. When I stop the serial communication, there are no "Range Check Error" boxes. What does this mean and how do I go about resolving it? I am looking for simple strategy. I know I could spend days checking line by line.

"Range Check Error" caused by improper assignment of values or accessing inaccessible index of an array. Am I correct?

回答1:

Your understanding of range check errors is correct. They arise when you access an array outside it's bounds. For example:

type
  TFixedArray = array [0..41] of Integer;
var
  a: TFixedArray;
begin
  a[42] := 1+2;//!! this is a range check error !!
end;

Or for a dynamic array:

var
  a: array of Integer;
begin
  SetLength(a, 666);
  a[665] := 12;//this is fine
  a[666] := 42;//!! this is a range check error !!
end;

I've illustrated this with assignment, but reading an array with an index out of bounds will also produce a range error.

The range error should report an address at which it occurs which you can then translate into a code location with your map file. Even better would be if you were using madExcept or some such tool.


UPDATE

Prompted by Ken, the documentation states what is affected by the range checking option as follows:

In the {$R+} state, all array and string-indexing expressions are verified as being within the defined bounds, and all assignments to scalar and subrange variables are checked to be within range.



回答2:

Having read other information about the concept of "range check error", I believe that the reason for causing the "range check error" in this scenerio is that: the variable assigning to access the serial port the program reads is an 16-bytes(or smaller) type, and the serial port the program reads exceeds the limitation of the variable. Notice that [When I stop the serial communication, there are no "Range Check Error" boxes.], this should make all things clear.



标签: delphi range