Recent

Author Topic: Access Violation on sample Program  (Read 7893 times)

LSemmens

  • New Member
  • *
  • Posts: 16
Access Violation on sample Program
« on: July 17, 2014, 10:34:58 am »
Linux mint 17 64bit
Lazarus 1.2.4
FPC 2.6.4

Am working my way though a tutorial on Lazarus and FPC all seems to work well until I attempt to access a function in a Unit that has been created. All that is intended is for the date and time to be displayed in a message box using the ShowMessage component of the Dialogs class (I think). It calls a function MyDT.GetDateTimeAsString which works fine.

function TMyDateTime.GetDateTimeAsString: string;
  begin
   Result:= DateTimeToStr(fDateTime);
  end;
If I replace fDateTime with now(), the function works fine.

now here is my issue

fDateTime is declared as private in the DateTime unit with the statement]

TMyDateTime = class
    private
      fDateTime: TDateTime;
    public                             

I seem to be going around in circles as I have double and triple checked that I have copied the code correctly It compiles correctly and only falls over when I click on a button. As Already stated, I seem to have isolated it to fDateTime.

Code attached

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Access Violation on sample Program
« Reply #1 on: July 17, 2014, 11:03:43 am »
Ug.  I just can't see a problem with your code, and am away from IDE at the moment so can't see step through it...

What happens if you replace
Code: [Select]
function TMyDateTime.GetDateTimeAsString: string;
  begin
   Result:= DateTimeToStr(fDateTime);
  end;
with
Code: [Select]
function TMyDateTime.GetDateTimeAsString: string;
  begin
   Result:= FloatToStr(fDateTime);
  end;
Sure, you won't get the value you're after, but for now it may to help clarify if DateTimeToStr is the problem or if there is something else going on...

And I presume the AV only occurs when you click on Button1?  Does clicking on Button2 and Button3 return the expected values?
« Last Edit: July 17, 2014, 11:05:59 am by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation on sample Program
« Reply #2 on: July 17, 2014, 11:53:29 am »
Perhaps removing the penultimate lines of your main form
Code: [Select]
initialization
{$I main.lrs}   
will help. But you would need to share the entire project (main menu: Project->Publish project, choose a new directory to save the published project in, and then zip the result) to see what is going wrong.

BTW you need to add an OnDestroy event handler for the main form
Code: [Select]
procedure TForm1.FormDestroy(Sender: TObject);
begin
  myDT.Free;
end;
to prevent a memory leak.

LSemmens

  • New Member
  • *
  • Posts: 16
Re: Access Violation on sample Program
« Reply #3 on: July 17, 2014, 01:00:49 pm »
Thanks for the responses. The problem occurs on all buttons. If I remove initialisation, the project throws up a "resourceless Form" message and fails.
There is a destroy handler at the end of the DateTimeUnit, if I add your code, Howardpc, to the end of the main form, it throws up a "method identifier expected" at position 31 of the procedure line and an "identifier not found" message on myDT.

Replacing
   Result:= DateTimeToStr(fDateTime);
with
   Result:= FloatToStr(fDateTime);

produces the same result.
edit:
If I replace
  Result:= DateTimeToStr(fDateTime);
with
  Result:= DateTimeToStr(now());

the button works as intended. As I am a novice with fpc and Lazarus, I'm certain it's something simple, but have been away from programming for about 30 years so OOP is a novelty. meantime I shall zip the entire project and attach it
 how can I attach the entire project as the tarball produced was 5.3Mb
« Last Edit: July 17, 2014, 01:17:14 pm by LSemmens »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Access Violation on sample Program
« Reply #4 on: July 17, 2014, 01:25:49 pm »
There must be something else wrong with your project.
This is what I did:

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  MyDT:= TMyDateTime.Create(Now);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  writeln(MyDT.GetDateTimeAsString);
  writeln(MyDT.GetDateAsString);
  writeln(MyDT.GetTimeAsString);
  MyDT.AddHours(1);
  writeln(MyDT.GetDateTimeAsString);
  MyDT.AddDays(1);
  writeln(MyDT.GetDateTimeAsString);
end;

It outputs:
Code: [Select]
C:\Users\Bart\LazarusProjecten>test
17-7-2014 13:22:53
17-7-2014
13:22:53
17-7-2014 14:22:53
18-7-2014 14:22:53

No AV.

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation on sample Program
« Reply #5 on: July 17, 2014, 01:27:47 pm »
(You can actually delete the overridden destructor for TMyDateTime, and its implementation, since it is useless, but harmless).

Try this. Start a new Lazarus project, and save it and name it appropriately in a new directory. Check that the empty project compiles and displays an empty form OK. Copy dateTimeUnit.pas (or whatever you have called the unit that declares TMyDateTime) to the new project directory.
Add a button and a button OnClick handler, and an OnCreate handler and and OnDestroy handler for the project's main form, and add dateTimeUnit to the uses clause.
Complete the implementations of the handlers as follows:

Code: [Select]
unit mainDateTime; // or whatever you have called it

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Forms, Dialogs, StdCtrls,
 dateTimeUnit; // or whatever you called it

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    myDT: TMyDateTime;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(MyDT.GetDateTimeAsString);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyDT:= TMyDateTime.Create(Now);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  myDT.Free;
end;

end.

If that works OK, then add the other buttons and handlers one by one, testing as you go until you have the full project working. Delete the first project, which has something wrong somewhere, but without seeing it we don't know where.

LSemmens

  • New Member
  • *
  • Posts: 16
Re: Access Violation on sample Program
« Reply #6 on: July 17, 2014, 03:38:12 pm »
Thanks for your efforts, sadly, howardpc, your suggestion also produced an access violation even after creating an entirely new project. Your solution, Bart, would be fine if I was working in a terminal window. I was attempting to learn the basics of OOP and using buttons, check boxes etc. to port an Access project that I have developed into MYSQL and FPC as a front end. I'll give up on this particular problem for now, as it is not really my primary focus, dates and times are not critical in my projected app. This is just part of my learning curve. I'm sure I'll find many (more "useful") problems to waste your time. Thanks for the assistance, anyway.

Can you recommend any good resources that will give me a solid grounding of FreePascal? My background has been dBase and Clipper, with some VBA in the last 10 years. So, procedural languages are more "logical" to me.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11458
  • FPC developer.
Re: Access Violation on sample Program
« Reply #7 on: July 17, 2014, 03:43:27 pm »
Pretty much all Delphi tutorials will provide insight. Search for "essential pascal" (and -delphi) by Marco Cantu

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Access Violation on sample Program
« Reply #8 on: July 17, 2014, 03:49:25 pm »
I realise you're moving on, but my curiosity about the original problem is peeked.  :) Any chance you can post the project?

Quote
(main menu: Project->Publish project, choose a new directory to save the published project in, and then zip the result)

Quote
This is just part of my learning curve.
As I said, couldn't find anything wrong with your code, so your learning curve is on the right track :)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation on sample Program
« Reply #9 on: July 17, 2014, 03:53:15 pm »
howardpc, your suggestion also produced an access violation even after creating an entirely new project.

In that case your present Lazarus installation is badly flawed. Uninstall it and clean and remove the directories that were created.
Then reinstall, and make sure you can successfully compile and run an empty project. No GUI development is possible without a working Lazarus IDE that compiles a bare-bones project to show you an empty resizeable form that has working minimise and close icons.

LSemmens

  • New Member
  • *
  • Posts: 16
Re: Access Violation on sample Program
« Reply #10 on: July 18, 2014, 01:25:56 pm »
I'm sorry that I have purged all copies of that project, if I have time, I shall re-create it and see where it takes me. I have managed to successfully compile other programs both before and after the one giving me grief, so I suspect that there is precious little wrong with my Lazarus installation.

If I publish my project and then compress it, will it produce a smaller file than my first attempt? (i.e. smaller than 5.3Mb that my first attempt produced - which was just a zip of the project folder and enclosed files)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Access Violation on sample Program
« Reply #11 on: July 18, 2014, 01:28:00 pm »
If I publish my project and then compress it, will it produce a smaller file than my first attempt? (i.e. smaller than 5.3Mb that my first attempt produced - which was just a zip of the project folder and enclosed files)
Yes it should, as it should not include binary files or .ppu or .o or .a files (or the lib/units dir which are unnecessary).
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018