Recent

Author Topic: Incorrect Proc being called ???  (Read 3756 times)

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #15 on: September 24, 2022, 06:51:24 pm »
Set break point and see where it goes.
That's exactly what I'm doing  :D
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #16 on: September 26, 2022, 10:35:58 am »
I've spent that past two days going around in circles ! :(

Having solved the original incorrect proc. call (to Gas rather than Elec) by re-naming all three 'save' procs, the problem re-appeared as a call to another totally unrelated proc.   Re-naming that moved the problem to yet another  >:( >:(   

The 'logic' seemed (to me) to be a fundamental incorrect 'offset' somewhere but I had no idea how many more 're-name - shift the issue' itterations might be created so I bit the Laz 2.2 bullet and moved back to Laz 1.6  - which had never shown the incorrect call issue.

Compilation was no problem BUT I did have a 'pop-up' telling me about a 'Change on Disk' which I inadvertantly clicked on [Reload from disc]   :(  -  I had intended to [Ignore] - the [Ignore] button on other such messages is on the right and I didn't notice (until after the fact) that in this case it was [Reload]  :-[    -    some of that 'detail' is a bit of a blur since I didn't make specific notes at the time.

Now I could look at converting the ElecRates file again, and all went well so all three Arrays are now dynamic.  I was therefore anticpating a straight-forward run so was somewhat surprised to get a crash with a SIGSEGV error,  which I've now traced to a call to reWrite a Data File  -  this is NOT the file which is created from the (now) dynamic array - it's a file that has previously (in the current 'run') been opened, read and closed!  (correctly - ie. the data is 'good')  The CallStack & Assembler windows are shown in the attached ScreenGrab 1.pdf.

I have routines which create backups of datafiles (naturally!) before they get re-written but I hadn't used such for this particular file (I can't understand why?) so I then did write such a call -- only to find that the SIGSEGV error has moved to 'assign' the name of the backup file ??? - CallStack & Assembler = ScreenGrab 2.pdf.

This project has been running perfectly well since 2016 with data being added every day, so you'll appreciate that these developements are extraordinarily difficult for me to understand and I really would appreciate any suggestions as to a way forward.


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Incorrect Proc being called ???
« Reply #17 on: September 26, 2022, 11:23:02 am »
Your errors looks very strange.

I have a couple of suggestions for you.

To be sure it isn't something with Lazarus configuration, I'd install new Lazarus in separate directory (with Fpcupdeluxe). Then I'd copy project to new location and try to see if there are still same errors occurring.

Probably isn't related, but when working with files I have a habit of using AssignFile and CloseFile instead of Assign and Close (also TextFile instead of Text if it is about text files).

If you still have problems with calling wrong procedures, you could change definition by adding parameters of different types to each of these procedures, something like this:
Code: Pascal  [Select][+][-]
  1. procedure SaveElecRates(DummyInt: Integer);
  2. ...
  3. procedure SaveGasRates(DummyChar: Char);
  4. ...
  5. procedure SaveFiTRates(DummyBoolean: Boolean);
  6.  

This is a bit desperate move, but it may help to surface real problem.

To me it looks there are some left overs after compilation. Did you maybe changed names of units or files?

How many units and forms are in your project (to get a grasp of project size)?

SIGSEGV error may be a new problem, unrelated to calling wrong procedure, maybe it got something to do with your change to dynamic arrays.
Check 'Use Heaptrc unit' in debugging options to see if you have memory leaks.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Incorrect Proc being called ???
« Reply #18 on: September 26, 2022, 11:38:55 am »
Opening a just closed file may file on some OSes as lock linger.

Does putting a sleep(5000) before that rewrite make a difference?

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Incorrect Proc being called ???
« Reply #19 on: September 26, 2022, 11:56:16 am »
Having solved the original incorrect proc. call (to Gas rather than Elec) by re-naming all three 'save' procs
You didn't solve it. You just moved the problem to another place.

(...), the problem re-appeared as a call to another totally unrelated proc.   Re-naming that moved the problem to yet another  >:( >:(   
Yeah, that's what I mean.

You probably have a really basic simple bug in your program somewhere (array range wrong or something else), where you write to places you shouldn't (overwriting important code).
But without seeing that bit of programming, none of us can pinpoint where exactly.

If the problem is in the reading and writing of the files (if the problem goes away if you don't read and write to files) you can create a new small project, but the routines there and put it here. Some of us can probably spot this in a matter of minutes (depending on your code).

PS. Did you enable the I/O, Range check, Overflow etc in Project options > Compiler options > Debugging
That's the first thing you could try. It'll throw an exception when you access something "wrong".


J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #20 on: September 26, 2022, 12:26:48 pm »
Opening a just closed file may file on some OSes as lock linger.

Does putting a sleep(5000) before that rewrite make a difference?
Interesting idea Marcov, thanks, but no.

The 'Open' is not immediately - nor even 'soon after' - the 'Close'  -  the Open, Read, Close takes place at program start up and the new 'Open' (reWrite) won't be called until some new data has been entered and eventually a [Save] button is clicked. With the fastest fingers in the world I couldn't do that in less than about 10 seconds and it would normally be something like a minute -  maybe much longer if I get distracted  :D

I did add a Sleep(10000) though,    just in case.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: Incorrect Proc being called ???
« Reply #21 on: September 26, 2022, 12:34:26 pm »
The 'Open' is not immediately - nor even 'soon after' - the 'Close'  -  the Open, Read, Close takes place at program start up and the new 'Open' (reWrite) won't be called until some new data has been entered and eventually a [Save] button is clicked.

Then you didn't show actual code. In your first post you showed code where you read ElecRates, close it and immediately after that you call SaveElecRates:

Here is the 'calling' code:
Code: Pascal  [Select][+][-]
  1.   procedure ReadElec;
  2.   Var
  3.     i: word;
  4.   begin
  5.     {$I-} reset(ElecFile); {$I+}
  6. [...]
  7.         while not eof(ElecRates) do
  8.           begin
  9. [...]
  10.             read(ElecRates,ElecList[i]);
  11. [...]
  12.                   inc(i);
  13.           end;
  14.  
  15.          close(ElecRates);     //  one off - conversion from Static to Dynamic Array
  16.          SaveElecRates;
  17.  

P.S.: And now I remember that it was strange to me why you save rates immediately after reading it.
« Last Edit: September 26, 2022, 12:36:21 pm by dseligo »

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #22 on: September 26, 2022, 02:03:03 pm »
Having solved the original incorrect proc. call (to Gas rather than Elec) by re-naming all three 'save' procs
You didn't solve it. You just moved the problem to another place.
You are correct of course  :-[
Quote from: rvk
You probably have a really basic simple bug in your program somewhere (array range wrong or something else), where you write to places you shouldn't (overwriting important code).
But without seeing that bit of programming, none of us can pinpoint where exactly.
I suspect that you are spot on.

Quote from: rvk
If the problem is in the reading and writing of the files (if the problem goes away if you don't read and write to files) you can create a new small project, but the routines there and put it here. Some of us can probably spot this in a matter of minutes (depending on your code).

PS. Did you enable the I/O, Range check, Overflow etc in Project options > Compiler options > Debugging
That's the first thing you could try. It'll throw an exception when you access something "wrong".
I hadn't got any of those optiones enabled  -  now I have ALL of them on and as you surmise !Bingo!   (thanks for that suggestion @rvk)   some calls to what are now dynamic arrays I had not changed from the static (<20)  to Max( X )Rec  :-[   That would certainly cause problems!   They are all now sorted.

With range checking on,  a very long outstanding error has come to light. It was never going to cause a problem - an issue where a blank date (d,m,y = 0,0,0 )  could be passed to a function which has an array starting at 1  :(  easily sorted with a check for zero -  but I'm also getting a range check error which doesn't make sense - - - -
Code: Pascal  [Select][+][-]
  1. ElecUsed[3] := Elec_Data[YearIndex,CurrWeek] - Elec_Data[YearIndex,  1];
- - - - triggers a 201 (RC)   but at this time
YearIndex = 3
CurrWeek = 39
whilst
Code: Pascal  [Select][+][-]
  1. Elec_Data : array[1..3,1..53] of word;
  2. ElecUsed  : array[1..4] of word;

39 is certainly within the range 1..53 just as 3 is in the range 1..3 (and 1..4) so I can't see why there is a range check error.

I could ignore (or turn off)  Range Checking  but I would sooner keep it in place in the hope that it will solve the issue.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Incorrect Proc being called ???
« Reply #23 on: September 26, 2022, 02:07:59 pm »
With range checking on,  a very long outstanding error has come to light. It was never going to cause a problem - an issue where a blank date (d,m,y = 0,0,0 )  could be passed to a function which has an array starting at 1  :(  easily sorted with a check for zero -  but I'm also getting a range check error which doesn't make sense - - - -
Code: Pascal  [Select][+][-]
  1. ElecUsed[3] := Elec_Data[YearIndex,CurrWeek] - Elec_Data[YearIndex,  1];
- - - - triggers a 201 (RC)   but at this time
YearIndex = 3
CurrWeek = 39
whilst
Code: Pascal  [Select][+][-]
  1. Elec_Data : array[1..3,1..53] of word;
  2. ElecUsed  : array[1..4] of word;
You need to look at the values inside Elec_Data[3, 39] and Elec_Data[3,  1].
If the second if bigger than the first you are going into negative values which can't be stored in Word (resulting in a range check error).

So look at the values just before you execute that line.

I could ignore (or turn off)  Range Checking  but I would sooner keep it in place in the hope that it will solve the issue.
You could keep those on while developing.
Before you actually distribute your program, you can turn it off.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #24 on: September 26, 2022, 02:22:49 pm »
You need to look at the values inside Elec_Data[3, 39] and Elec_Data[3,  1].
If the second if bigger than the first you are going into negative values which can't be stored in Word (resulting in a range check error).

So look at the values just before you execute that line.

Spot on rvk !!!  D'oh!   those figures are checked before being accepted and [3,1] can never be greater than [3,39]  -   the figures are the readings in week 1 and week 39 and they always advance (unless my electric meter moves backward ?? :D 

HOWEVER  -  this routine is also called before the current week's value has been entered and is therefore ZERO !!!  --- in my defence, this program was written some time ago and I have progressed somewhat in my ability since then  :-[

Quote from: rvk
I could ignore (or turn off)  Range Checking  but I would sooner keep it in place in the hope that it will solve the issue.
You could keep those on while developing.
Before you actually distribute your program, you can turn it off.
There will be no 'distribution :)  -  it's purely a private record for my personal gratification.

I'll do some more 'adjustments' and report back  -  I think I may even return to Laz 2.2 to see if the original problem has really disappeared.

I may not get back 'til tomorrow - I have a medical appointment in half an hour and then a choir rehearsal - - - - there arn't enough hours in the day !

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Arioch

  • Sr. Member
  • ****
  • Posts: 421
Re: Incorrect Proc being called ???
« Reply #25 on: September 26, 2022, 02:59:35 pm »
used to have a similarly looking weirdness in Delphi

Delphi is not FPC down there, so no exact parallels, but still something could be similar. I had a number of cases when binary files did not match sources and debug info, or sources did not match binaries adnd debug info.

In the end, the Pascal code i saw running in IDE was not the code that actually was executed (and seen in assembler window). Yes, it was royally conusing.

I used SysInternals Process Monitor eventually, to have a list of source and intermediate (.DCU, .DCP, here it probably would be  .PPU, .LIB .OBJ) files used in both compilation and debugging session and compare them. And also library binaries (BPL, this does not apply to FPC i think).

One case was Delphi XE2.4 installer is broken. It installes IDE extension that copies RTL XE2.3 into Windows folder. This made any program linked in "binary packaes" mode be compiled against newer RTL and on the run loadign the older RTL. This mismatch manifested in many small incompatibilities and fuzzy behavior, and when i tried to debug - IDE loadded debuginfo and sources from XE2.4 TRL while running XE2.3 RTL binaries. ProcMon allowed me to noitce, that linker used one set of BPLs while debugger used others.

Other cases where runtime packages, the project consisted off. There was not unified structure, all the packageswere made ad hoc by different people and with different projetc options. As i was unifyign it all, i had many cases that either i forgot to rebuild each and every binary package after some source changed, or the freshly compiled units of ProjectA were put in one folder, yet ProjectB picked stale files of ProjectA from another folder. Again, ProcMon made me see it. Clean and rebuild was not helping those cases, as related projects had different folders

Other cases where same-named source files, placed in different folders, some several years older than others. On top of that, {$ifDef} were switchig on and off big parts of those sources, differently for different projects. Both Delphi IDe and debugger were royally confused to have several different copies of same-named source units loaded into one process and exectued. The Pascal sources i was seeing were diferent (if same-named) files that the one compiled into code being executed.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #26 on: September 27, 2022, 04:04:52 pm »
Latest development in this Saga !   - - -   

With Range Checking ON I have discovered some disturbing information about my past, essentially cavalier, attitude toward array addressing :-[

This project not only records energy usage readings but also does comparisons with historic data - overall potentially very useful information.  What I’ve now discovered - that was not a problem when using Laz 1.6 - is that comparisons previous to the first day of the first year for which data is available - ie. A ZERO index to a static array starting at 1 - were not trapped  :-[

Since I have data available for every day or week since 2015 I have [Prev] & [Next] Day, Month & Year buttons but I restrict the ‘live’ data to the past three years since anything beyond that is unlikely to be useful. Therefore there was always the possibility that the [Prev]... button would call for data which was outside that available but I hadn’t trapped the potential ‘out of range’ D’oh!

I got the project working without Range Check Errors in Laz 1.6 and then re-opened it in Laz 2.2  - eventually - after I had found ALL of the bad array referencing  :D  - I’ve also got it working under Laz 2.2.

So (to some extent, ‘as expected’ ), the calls to incorrect proc’s was due to the code being stamped over by writing Data outside of some allocated array space. It certainly accounts for the ‘Weird’ behaviour

I haven’t completely updated the code as yet - there are now other issues of data not being displayed or missing but at least I can work on tracing those - they are pretty certain to be down to the loss of previously corrupt data so it will just be a matter of stepping through the various routines a line at a time, tedious, but necessary.

A small irritation that I would appreciate someone’s opinion on, is the fact that upon program exit I get a ‘HeapTrace’ Error which appears to be a contradiction - the attached .PNG file ought to be self explanatory . . .     The nubers all seem to agree so why is it flagged as an error ?

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Incorrect Proc being called ???
« Reply #27 on: September 27, 2022, 04:28:27 pm »
I was very tempted to suggest cavalier typecasting of arrays after somebody (you know who you are :-) was spotted doing something dodgy a few days ago.

I'm slightly stumped at to how this was actually going wrong though, unless you're passing the affected array as a parameter to a function by value somewhere.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Incorrect Proc being called ???
« Reply #28 on: September 27, 2022, 04:35:27 pm »
A small irritation that I would appreciate someone’s opinion on, is the fact that upon program exit I get a ‘HeapTrace’ Error which appears to be a contradiction - the attached .PNG file ought to be self explanatory . . .     The nubers all seem to agree so why is it flagged as an error ?
Yeah, it's not actually an error. It's just a notification.
In Delphi you could have a similar dialog but it will only surface when the is an actual leak.
With heaptrc it always gives you a dialog.
The 0 unfreed shows you there are no leaks.

(Maybe there is a hack to not show the dialog when there is no leak but I'm not sure)

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Incorrect Proc being called ???
« Reply #29 on: September 27, 2022, 04:43:43 pm »
I was very tempted to suggest cavalier typecasting of arrays after somebody (you know who you are :-) was spotted doing something dodgy a few days ago.

I'm slightly stumped at to how this was actually going wrong though, unless you're passing the affected array as a parameter to a function by value somewhere.

MarkMLl
I don't think I have ever 'passed an array as a parameter'  -  I would be hard pressed to even describe what that actually meant :)

I also can't really comprehend how asking for a value from a memory location beyond the array bounds would cause such devastaion. I don't think that I actually wrote data to an out of range address - I'll have to check that to be 100% though  -  all I can say for sure is that once I'd stopped out of range addressing, the problems went away.

I did find one call that requested info. from [-7,1] but that was in a test proc. and never part of the original program.


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018