Forum > Beginners

Access Violation on sample Program

(1/3) > >>

LSemmens:
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:
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: ---function TMyDateTime.GetDateTimeAsString: string;
  begin
   Result:= DateTimeToStr(fDateTime);
  end;
--- End code ---
with

--- Code: ---function TMyDateTime.GetDateTimeAsString: string;
  begin
   Result:= FloatToStr(fDateTime);
  end;
--- End code ---
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?

howardpc:
Perhaps removing the penultimate lines of your main form

--- Code: ---initialization
{$I main.lrs}   

--- End code ---
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: ---procedure TForm1.FormDestroy(Sender: TObject);
begin
  myDT.Free;
end;

--- End code ---
to prevent a memory leak.

LSemmens:
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

Bart:
There must be something else wrong with your project.
This is what I did:


--- Code: ---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;

--- End code ---

It outputs:

--- Code: ---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

--- End code ---

No AV.

Bart

Navigation

[0] Message Index

[#] Next page

Go to full version