[fpc-pascal] RPos Causing Access violation
James Richters
james at productionautomation.net
Tue May 21 14:18:30 CEST 2019
This is my function that seems to cause this more than anything else… it’s ridiculously simple…
Function ExtractFilePathAndNameWithoutExt(Filenametouse:String):String;
Begin
ExtractFilePathAndNameWithoutExt := copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1);
End;
I stuck a bunch of Writeln’s in it to try to figure out what the heck is happening… my normal response when I need to track something down and the debug information is not clear.
Function ExtractFilePathAndNameWithoutExt(Filenametouse:String):String;
Begin
Writeln('Filenametouse: ',Filenametouse);
Writeln('ExtractFileExt(Filenametouse): ',ExtractFileExt(Filenametouse));
Writeln('rpos(ExtractFileExt(Filenametouse),Filenametouse): ',rpos(ExtractFileExt(Filenametouse),Filenametouse));
Writeln('Length(Filenametouse): ',Length(Filenametouse));
Writeln('copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1): ',copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1));
ExtractFilePathAndNameWithoutExt := copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1);
End;
The results I get when it fails are :
Writeln('Filenametouse: ',Filenametouse); This gives me what I expect: a valid filename with full path
Writeln('ExtractFileExt(Filenametouse): ',ExtractFileExt(Filenametouse)); This gives me what I expect: the extension only from the above file name
Writeln('rpos(ExtractFileExt(Filenametouse),Filenametouse): ',rpos(ExtractFileExt(Filenametouse),Filenametouse)); This is what’s causing the error… strangely it does the Writeln AFTER the error.. I don’t understand how that can even happen.
I tested this selecting the same 3 files over and over until it failed. Here is the Console output showing the last good set and the failre:
-------------------------------------- Console Output-----------------------------
M:\
elipse_1.dpax
elipse_2.dpax
elipse_3.dpax
4 Files Selected
Filenametouse: M:\elipse_1.dpax
ExtractFileExt(Filenametouse): .dpax
rpos(ExtractFileExt(Filenametouse),Filenametouse): 12
Length(Filenametouse): 16
copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1): M:\elipse_1
Drawing: M:\elipse_1
Filenametouse: M:\elipse_2.dpax
ExtractFileExt(Filenametouse): .dpax
rpos(ExtractFileExt(Filenametouse),Filenametouse): 12
Length(Filenametouse): 16
copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1): M:\elipse_2
Drawing: M:\elipse_2
Filenametouse: M:\elipse_3.dpax
ExtractFileExt(Filenametouse): .dpax
rpos(ExtractFileExt(Filenametouse),Filenametouse): 12
Length(Filenametouse): 16
copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1): M:\elipse_3
Drawing: M:\elipse_3
M:\
elipse_1.dpax
elipse_2.dpax
elipse_3
4 Files Selected
Filenametouse: M:\elipse_1.dpax
ExtractFileExt(Filenametouse): .dpax
An unhandled exception occurred at $0040ED1A:
EAccessViolation: Access violation
$0040ED1A
$004026DB main, line 119 of i:/programming/gcode/mill/plotdraw.pax.pas
rpos(ExtractFileExt(Filenametouse),Filenametouse):
--------------------------------------End Console Output-----------------------------
Does anyone have any idea at all why RPOS could not determine that the position of .dpax in M:\elipse_1.dpax was 12 like every other time I ran this????????????
I ran this while looking at task manager and it starts up with 9.2MB of memory and that stays consistent, so I don’t think I have a memory leak anywhere. Any ideas?? I’m running on Windows 10, compiling with FPC 3.0.4RC1 with the FPC text IDE.
James
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of James Richters
Sent: Tuesday, May 21, 2019 7:35 AM
To: 'FPC-Pascal users discussions' <fpc-pascal at lists.freepascal.org>
Subject: [fpc-pascal] Debug Advice needed
I have this program that is confounding me. It obtains a file name with GetOpenFileNameA then it processes the file and draws stuff on the screen, then loops back and gets another file name until GetOpenFileNameA returns a false… so as long as I keep giving it files it keeps running and if I cancel the file selection, it exits. It will run in a loop like this various numbers of times… sometimes 4 times, sometimes 8 times, sometimes twice, sometimes 30 times and then I get this access violation like this:
An unhandled exception occurred at $0040ED07:
EAccessViolation: Access violation
$0040ED07
$00411A6C
$00411F25
$00402255 PLOTDRAWPAX, line 76 of i:/programming/gcode/mill/plotdraw.pax.pas
$00402705 main, line 119 of i:/programming/gcode/mill/plotdraw.pax.pas
Line 76 is the a call to a procedure in another unit that’s very complicated and calls a lot of other functions and procedures.
I keep testing with the same 5 files over and over, and each file has processed fine many times so there isn’t a problem with the data in my files or the way I am processing it…. and there doesn’t seem to be a particular file that causes this. I’m thinking I have some memory issue or something is not freeing or ?
It’s also not always the same exact message.. sometimes I get
An unhandled exception occurred at $0040ED07:
EAccessViolation: Access violation
$0040ED07
$004026DB main, line 118 of i:/programming/gcode/mill/plotdraw.pax.pas
Line 118 is a call to a function that is in no way related to the procedure that is called on line 76… it’s actually a ridiculously simple function. Exactly one line of code to it, and I have run that function millions of times with other programs that use the same unit.
The FPC text IDE is reporting runtime error 217 which doesn’t tell me much.. just an unhandled exception occurred.
My question is… how do I track these kinds of things down? Is there a way I can use the hex addresses to see more precisely what caused the error?
I’m confused why sometimes I get very detailed debug reports that get me to the exact command that caused the problem and other times I have some information but then it just stops and I have nothing following the hex addresses.
James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20190521/87595462/attachment.html>
More information about the fpc-pascal
mailing list