5

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?

ThN
  • 3,235
  • 3
  • 57
  • 115
  • 1
    Can you install Delphi on the other machine and debug it? – Blorgbeard Mar 28 '11 at 16:12
  • Better yet, I am thinking of narrowing down to the offending line of code by commenting out sections of code and recompiling. Then, test run the program on the Window 7 PC. I know it is going to be tedious, but I think I know what part of the program this is taking place. – ThN Mar 28 '11 at 16:31
  • Why don't you find the offending line with the map file as I suggested? – David Heffernan Mar 28 '11 at 19:24
  • 1
    See more on Range Checking here: https://stackoverflow.com/questions/10134658/how-to-set-default-compiler-options-for-xe2/75578536#75578536 – Gabriel Feb 27 '23 at 09:41

2 Answers2

8

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Just an addendum: 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. Note the `string-indexing` and `scalar and subrange variables`. – Ken White Mar 28 '11 at 16:28
  • your examples are fantastic, but do you have to use "666". LOL. – ThN Mar 28 '11 at 16:28
  • @digitalanalog - why not, you will better remember that :) –  Mar 28 '11 at 16:34
  • Can Range Check Error lead to memory leak? Since the error is suppressed when you don't check "Range Checking" under Runtime Error, I am sure the error is still raised but not displayed in the message box. – ThN Mar 28 '11 at 19:17
  • @digitalanalog Accessing arrays out of bounds could lead to just about any failure mode you could think of! All bets are off once you start poking at random bits of memory. – David Heffernan Mar 28 '11 at 19:23
  • Already found the bug. My string parser was checking every character in a string variable and the length of the string in an if conditional block. Thus, it raised the Range Error Check. It is running fine now...Until next time, thanks. – ThN Mar 28 '11 at 20:16
1

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.

Edward
  • 53
  • 5