RPos Causing Access violation

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

RPos Causing Access violation

Zaaphod

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 <[hidden email]> On Behalf Of James Richters
Sent: Tuesday, May 21, 2019 7:35 AM
To: 'FPC-Pascal users discussions' <[hidden email]>
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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RPos Causing Access violation

Marco van de Voort-2

Op 2019-05-21 om 14:18 schreef James Richters:

>
> 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.
>
>
Start with assigning the result of the rpos to a variable and printing
and reusing that.

To do anything we will need a compilable short fragment of code
demonstrating the bug.

Sometimes this can be done by just taking the existing source and start
removing code that doesn't seem to influence the code, and passing
constants instead of whereever you get your filenames.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RPos Causing Access violation

Zaaphod
I'll try your ideas on how to track this down.

I wonder if I am really having some kind of other issue... maybe RPOS is using the stack and I'm running out of space or some other kind of memory issue.
It seems odd  that I can run this routine dozens of times successfully and then I get this access violation...  with the exact same input it was fine with one loop earlier.  I also have access violations in other procedures.  
I have a sequence that produces it here... but other sequences produce it in other places.     Is there a way I can output various memory areas to see if it's really a memory related issue?  Something like a writeln for stack space etc?

James

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Marco van de Voort
Sent: Tuesday, May 21, 2019 9:51 AM
To: [hidden email]
Subject: Re: [fpc-pascal] RPos Causing Access violation


Op 2019-05-21 om 14:18 schreef James Richters:

>
> 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.
>
>
Start with assigning the result of the rpos to a variable and printing and reusing that.

To do anything we will need a compilable short fragment of code demonstrating the bug.

Sometimes this can be done by just taking the existing source and start removing code that doesn't seem to influence the code, and passing constants instead of whereever you get your filenames.

_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RPos Causing Access violation

Bart-48
In reply to this post by Zaaphod
On Tue, May 21, 2019 at 2:18 PM James Richters
<[hidden email]> wrote:

> Function ExtractFilePathAndNameWithoutExt(Filenametouse:String):String;
>
> Begin
>
>    ExtractFilePathAndNameWithoutExt := copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)-1);
>
> End;


From LazFileUtils unit:

function ExtractFileNameWithoutExt(const AFilename: string): string;
var
  p: Integer;
begin
  Result:=AFilename;
  p:=length(Result);
  while (p>0) do begin
    case Result[p] of
      PathDelim: exit;
      {$ifdef windows}
      '/': if ('/' in AllowDirectorySeparators) then exit;
      {$endif}
      '.': exit(copy(Result,1, p-1));
    end;
    dec(p);
  end;
end;

writeln(ExtractFileNameWithoutExt('c:\foo\bar\foobar.ext'));
gives:
c:\foo\bar\foobar

--
Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: RPos Causing Access violation

Zaaphod
Thanks for the function.   I don't think that my function is the problem at all.. I've just completed a test where I bypassed my file list and ran it several hundred times and my function never caused any problems.... I think I have a fundamental problem with the function I disabled to get it to work... maybe I am not cleaning up or resetting the way I should be.      I'm going to post that function with a separate title to make it clear it's a new topic

James

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Bart
Sent: Tuesday, May 21, 2019 12:50 PM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] RPos Causing Access violation

On Tue, May 21, 2019 at 2:18 PM James Richters <[hidden email]> wrote:

> Function
> ExtractFilePathAndNameWithoutExt(Filenametouse:String):String;
>
> Begin
>
>    ExtractFilePathAndNameWithoutExt :=
> copy(Filenametouse,1,rpos(ExtractFileExt(Filenametouse),Filenametouse)
> -1);
>
> End;


From LazFileUtils unit:

function ExtractFileNameWithoutExt(const AFilename: string): string; var
  p: Integer;
begin
  Result:=AFilename;
  p:=length(Result);
  while (p>0) do begin
    case Result[p] of
      PathDelim: exit;
      {$ifdef windows}
      '/': if ('/' in AllowDirectorySeparators) then exit;
      {$endif}
      '.': exit(copy(Result,1, p-1));
    end;
    dec(p);
  end;
end;

writeln(ExtractFileNameWithoutExt('c:\foo\bar\foobar.ext'));
gives:
c:\foo\bar\foobar

--
Bart
_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal