Recent

Author Topic: Access Violation  (Read 4480 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Access Violation
« on: December 05, 2018, 07:35:46 pm »
Getting an Access Violation at Line 14 and have no idea why:
Any suggestions?

Can post the entire program if need be,


 
Code: Pascal  [Select][+][-]
  1. function TForm1.ReplaceSimiColan(aRec : AirportRec) : AirportRec;
  2. Var
  3.  ARRec : AirportRec;
  4.  Line : String = '';
  5.  CTR : Integer = 0;
  6.  IDX : Integer = 1;
  7.  Lgth : Integer;
  8.  Simi : String = '';
  9. begin
  10.   ARRec := aRec;
  11.   Lgth := Length(ARRec.WrkLine);
  12.  
  13.     While IDX < Lgth do begin
  14.    Simi := Line[Idx];
  15.      if Simi = ';' then begin Line[Idx] := ' '; end;
  16.       Inc(Idx);
  17.    end;
  18. end;
  19.                                    

Note:

// 0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG

What I'm trying to do is replace the ; in the line with a space;
« Last Edit: December 05, 2018, 07:45:02 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Access Violation
« Reply #1 on: December 05, 2018, 08:09:41 pm »
From the source code I would say you missed an assignment to Line, maybe
Code: Pascal  [Select][+][-]
  1.  Line := ARRec.WrkLine;

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: Access Violation
« Reply #2 on: December 05, 2018, 08:25:17 pm »
Ansistrings are zero based

Edit - oops, no, ansistrings actually start at 1, same as shortstrings.

But as your string "line" is empty, the internal pointer representation is Nil. Accessing element 1 will therefore fail.
« Last Edit: December 05, 2018, 09:09:38 pm by Nitorami »

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: Access Violation
« Reply #3 on: December 05, 2018, 09:45:55 pm »
why invent wheel?
result:=stringreplace(ARRec.WrkLine, ' ; ', '   ',  [rfReplaceAll]);

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Access Violation
« Reply #4 on: December 05, 2018, 11:14:22 pm »
"why invent wheel?
result:=stringreplace(ARRec.WrkLine, ' ; ', '   ',  [rfReplaceAll]);"

Didn't Work.

So I tried this:

Str := ARRec.WrkLin;
Str2 := stringreplace(ARRec.WrkLine, ' ; ', '   ',  [rfReplaceAll]);

Didn't Work. Still Have the SimiColons;
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Access Violation
« Reply #5 on: December 05, 2018, 11:28:43 pm »
Didn't Work. Still Have the SimiColons;
I don't know what you are doing. It is working, for sure:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses  
  3.   SysUtils;
  4. var
  5.   str: String = '0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG';
  6. begin
  7.   WriteLn('before: ', str);
  8.   str := StringReplace(str, ';', ' ', [rfReplaceAll]);
  9.   WriteLn('after:  ', str);
  10.  
  11.   ReadLn;
  12. end.  
Quote
before: 0B7;Warren-Sugarbush;44.11738;-72.82702;1470;783;KPBG
after:  0B7 Warren-Sugarbush 44.11738 -72.82702 1470 783 KPBG

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation
« Reply #6 on: December 05, 2018, 11:37:38 pm »
You're changing a local variable in your function, rather than the actual string in the record.

Try this:
Code: Pascal  [Select][+][-]
  1. function TForm1.ReplaceSemiColon(aRec: AirportRec): AirportRec;
  2. var
  3.   i: Integer;
  4. begin
  5.   Result := aRec;
  6.   for i := 1 to Length(Result.WrkLine) do
  7.     if Result.WrkLine[i] = ';' then
  8.       Result.WrkLine[i] := ' ';
  9. end;

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Access Violation
« Reply #7 on: December 05, 2018, 11:51:20 pm »
Or this:
Code: Pascal  [Select][+][-]
  1. function TForm1.ReplaceSemiColon(aRec: AirportRec): AirportRec;
  2. begin
  3.   Result := StringReplace(aRec.wrkLine, ';', ' ', [rfReplaceAll]);
  4. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Access Violation
« Reply #8 on: December 06, 2018, 12:09:52 am »
Thanks All;

Have tried all 3 and they all work.






 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018