Lazarus

Using the Lazarus IDE => Debugger => Topic started by: stephanweber on June 24, 2019, 12:37:47 pm

Title: How to find "Range check error" quickly?
Post by: stephanweber on June 24, 2019, 12:37:47 pm
Hi,

in my Lazarus project I get this runtime error, because I have this check active in my proj options (-Cr). I have also set "Show debug info (-vd).


However, how can I also get the code line in which this error happens, best wo. debugger. This is because currently I have not yet a debugger installed, and installations are always a bit difficult in my current Linux setup.
In Delphi I remember something like "goto error" or "find error".

Also with "Show line numbers" or "Show all procs on error" I get no more hints. The program recovers, because the error pops only up if I make an integer variable larger than 32K. But also changing the suspious variables to Int64 does not help.

Bye Stephan
Title: Re: How to find "Range check error" quickly?
Post by: devEric69 on June 24, 2019, 12:52:37 pm
If you know where is the line (say #num26) that throws this exception, one method might be to frame it like that, with a "defensive Exception management", just long enough to understand and correct you bug:

Code: Pascal  [Select]
  1. Try
  2.   your-buggy-line #num26 of code, here
  3. Except
  4.   on E: EOutOfRange do Application.processmessages; (* <-- you set a breakpoint there, and you'll inspect your numerical variables, i, strings[j], ...,
  5. which are used by your line #num26, and understand the one that exceeds its boundary.
  6. Once the bug will be fixed, you'll delete the try...except *)
  7. end;
Title: Re: How to find "Range check error" quickly?
Post by: Martin_fr on June 24, 2019, 01:10:20 pm
-vd will not help at all

You should include "debugger info" (preferably of type "dwarf"):  -gw

You can add  -gl 
then when your running executable hits the error, it should print a stacktrace with line numbers.
(Run your app from a console, so you see the output / or try the "view" > "debug windows" > "console output")


I am not sure what your problem is to install a debugger. GDB is usually easily available for linux.

If your system is intel/amd based, then install the package LazDebuggerFp.
This gives you a debugger that does NOT need gdb.
Title: Re: How to find "Range check error" quickly?
Post by: stephanweber on June 25, 2019, 03:13:38 pm
Thanks. I found the problem with some statements like "Showmessage('here we are: begin of blablabla');"
Now I have to find an infinite loop, which seems to be triggered randomly (there is indeed a random number generation involved).

Bye Stephan
Title: Re: How to find "Range check error" quickly?
Post by: Thaddy on June 26, 2019, 10:12:38 am
If your two issues are related (as they seem to me) use RandomFrom or RandomRange instead of system.Random.
Also, precisely define define a range as type ( e.g. type myrange i= 3..9 ). That combination will prevent you from making range errors at run-time.
See: defensive programming in the wiki. https://wiki.freepascal.org/Defensive_programming_techniques

(reminder to self: upload the rest, finally, lazy b*sta*rd)
Title: Re: How to find "Range check error" quickly?
Post by: stephanweber on June 27, 2019, 02:16:32 pm
This was very helpful now:

you should include "debugger info" (preferably of type "dwarf"):  -gw

You can add  -gl 
then when your running executable hits the error, it should print a stacktrace with line numbers.
Run your app from a console!!!

Thanks a lot!!