[Solved] Access violation trying to create a timer lazarus / 21-General ---------------------------------------------------------------------- Wilko500 · 2025-06-11 23:48 Following on from my last post https://forum.lazarus.freepascal.org/index.php/topic,71422.0.html the timer works as it should when started by a button on the main form, starts the timer and displays the date/time on main form Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TfrmMain.Button1Click(Sender: TObject);Var    MyTest: TMyTimer;begin  MyTest.Create;    MyTest.Start(1000, @DisplayTime);end;  But I want it to start automatically when program starts so I put a call at the end of form.Activate and then got an Access violation.  I had something like this before when I tried to do something before the form was fully loaded/ready so I recoded to call the same code with Application.QueueAsyncCall at the end of Form.Activate.  This calls the same code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure TfrmMain.StartTimers;Var    MyTest: TMyTimer;begin    MyTest.Create;  //<=  Access violation here    MyTest.Start(1000, @DisplayTime);End;{Procedure Timers} which still gives an access violation Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i exception class 'Process stopped with reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)'. In file 'timerunit.pas' at line 30:FTimer := TTimer.Create(nil); I am very puzzled as to why the same code works from a button but fails when run from Application.QueueAsyncCall. ---------------------------------------------------------------------- dseligo · 2025-06-12 00:27 You don't create TMyTimer correctly. Do it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure TfrmMain.StartTimers;Var    MyTest: TMyTimer;begin    MyTest := TMyTimer.Create;  // <=  see this line    MyTest.Start(1000, @DisplayTime);End;{Procedure Timers} ---------------------------------------------------------------------- Wilko500 · 2025-06-12 17:37 Thank you @dseligo.  That's a duh-rr moment! And of course it's obvious when I look at it now.  Because the incorrect code, that I copied,  was working I was looking for a more complicated solution rather than the obvious. And it turns out that I can run the StartTimers procedure from form.Activate as I wanted to.