Recent

Author Topic: Panic: error: Access violations  (Read 3874 times)

otherside

  • New member
  • *
  • Posts: 9
Panic: error: Access violations
« on: January 23, 2017, 07:04:34 pm »
I installed Lazarus 1.6 and set the directory when opening it for the first time to:
(http://i.imgur.com/wUWlYqmm.jpg)

I got the following error:
(http://i.imgur.com/gfwNarcm.jpg)

This is the code I'm trying to execute, it's far from complete:

Code: [Select]
program project1;
uses unit1;
{procedure TStringList = class(TStrings);}

Type
      videoNamesArray = Array of String; {dynamic array; size initially set to 0}

  Var
      a : videoNamesArray;
      totalLines: Integer; {why can't I define this locally, instead of globally?}
      s: String;

      {this function will count how many lines are in the textFile
      and will assign that as the length of the dynamic array: videoNamesArray}
      function lineCount(fileName: String): Integer;
      Var f: textfile;
          {totalLines: Integer;} {why can't I define this locally, instead of globally?}
      begin
         assignFile(f, filename);
         reset(f); {open for reading}
         totalLines:=0;
         While NOT eof(f) do begin
           readln(f);
           inc(totalLines);
         end;
         closeFile(f);
         lineCount := totalLines;
      end;

{function transferToArray (videoNamesArray: a; fileName: String)    }

begin
    {writeln ('Hello, world.');}

    memo1.Clear;
    Str(lineCount('C:\Program Files (x86)\ReNamer\Scripts\filenames.txt'), s);
    memo1.lines.add(s);
    {readln }
end. 

I do have to head off to work so I apologize if my question isn't as detailed as it should be.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Panic: error: Access violations
« Reply #1 on: January 23, 2017, 07:14:33 pm »
Someone once said: everything that can go wrong, will go wrong  :D

Perfect example of that was shown above, no offense.

1. lazarus is unable to locate your lazarus installation directory (heavens knows if it is able to locate your FPC directory).
2. you are meddling with your project file
3. you are directly accessing things like a memo, while this should be part of your form.

Me thinks, that would be enough for starters. Fix, it. try again and come back if things fail (please show your code again in that case).

PS: in case you need more detailed instructions, then please tell what it is that you are trying to accomplish (using words), and please start of by telling us if you wish to generate a Full fledged LCL GUI application or simply a (commandline) FPC application.
« Last Edit: January 23, 2017, 07:26:24 pm by molly »

otherside

  • New member
  • *
  • Posts: 9
Re: Panic: error: Access violations
« Reply #2 on: January 24, 2017, 07:08:35 am »
I'm looking to generate a simple command line program.

The Lazarus.lpi file is located in the following directory:
D:\Install\Programs\Lazarus\ide\

Hench, I changed it from the default, .\, to that; during the 2nd installation.

In the first installation, Unit1 couldn't be found after following the direction here: http://forum.lazarus.freepascal.org/index.php?topic=24708.0

I've gone to Tools → Options → Environment → File → Lazarus directory (default for all project)

I've changed it from:
D:\Install\Programs\Lazarus\

To:
D:\Install\Programs\Lazarus\

It now gives the error: project1.lpr(2,6) Fatal: Cannot find unit1 used by project1 of the Project Inspector.
(http://i.imgur.com/ysgZ5zHm.jpg)

I guess this is major progress than, and probably warrants a new thread, but I'll stick on this thread for now.

Quote
  Project
  -> select Project Inspector
     -> click + Add
        -> in New Requirement select LazUtils from "Package Name"
           -> Create New Requirement

Still getting the same error after following these steps.

I got the program to work by doing away with Unit1 completely. I'm afraid there's some sort of memory leakage, since I had to do away with memo1.clear et al.

Code: [Select]
program project1;
{procedure TStringList = class(TStrings);}

Type
      videoNamesArray = Array of String; {dynamic array; size initially set to 0}

  Var
      arr : videoNamesArray;
      totalLines: Integer; {why can't I define this locally, instead of globally?}
      s: String;

      {this function will count how many lines are in the textFile
      and will assign that as the length of the dynamic array: videoNamesArray}
      function lineCount(fileName: String): Integer;
      Var f: textfile;
          {totalLines: Integer;} {why can't I define this locally, instead of globally?}
      begin
         assignFile(f, filename);
         reset(f); {open for reading}
         totalLines:=0;
         While NOT eof(f) do begin
           readln(f);
           inc(totalLines);
         end;
         closeFile(f);
         lineCount := totalLines;
      end;

{function transferToArray (videoNamesArray: a; fileName: String)    }

begin
    {writeln ('Hello, world.');}

    Str(lineCount('C:\Program Files (x86)\ReNamer\Scripts\filenames.txt'), s);

    writeln('Total lines in file: ', s);
    readln
end.   
« Last Edit: January 24, 2017, 07:34:33 am by otherside »

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Panic: error: Access violations
« Reply #3 on: January 24, 2017, 07:56:25 am »
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses classes;
  3. var
  4.    VideoNames:TStringlist;
  5.    LineCount:integer = 0;
  6. begin
  7.     VideoNames := TStringlist.Create;
  8.     try
  9.       VideoNames.LoadFromFile('C:\Program Files (x86)\ReNamer\Scripts\filenames.txt');
  10.       LineCount := VideoNames.Count;
  11.       writeln(LineCount);
  12.     finally
  13.       VideoNames.Free;
  14.     end;
  15.     readln;
  16. end.
Specialize a type, not a var.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Panic: error: Access violations
« Reply #4 on: January 24, 2017, 03:53:04 pm »
I'm looking to generate a simple command line program.
ok, thank you for the elaboration. It helps.

Quote
The Lazarus.lpi file is located in the following directory:
D:\Install\Programs\Lazarus\ide\
Quote
t now gives the error: project1.lpr(2,6) Fatal: Cannot find unit1 used by project1 of the Project Inspector.
(http://i.imgur.com/ysgZ5zHm.jpg)

Still getting the same error after following these steps.
Assuming you are using latest Lazarus IDE (Lazarus 1.6.2 with Free Pascal 3.0) you should not add anything manually as you just showed.

The 'normal' way of doing things is that you start lazarus IDE, create a new command-line project and save that project into a separate directory, preferably away from your lazarus installation (as far as possible).

Every other unit that you place into that same project directory will be automatically recognized (and included once asked for it in uses clause) by Lazarus/FPC.

Default FPC/Lazarus system units are found based upon your configuration settings. In case you compile your project using one of the RTL units and that fails then something is wrong with either your installation or your configuration. Things should compile for you out of the box.

In case more information is required, then please state your host platform, and version numbers of Free Pascal and Lazarus. Also state for which target your are trying to compile (the latter is the only other possible way that you should be able to retrieve errors with being unable to locate rtl units as they aren't compiled for your target yet. That is, in case host and target platform differs).

Other than that, Thaddy's code is exactly what i would propose as well. Unless you intended to learn something aout and/or experiment with reading files manually.
« Last Edit: January 24, 2017, 04:34:49 pm by molly »

 

TinyPortal © 2005-2018