ThN
ThN

Reputation: 3278

TTimer Stops by itself

I am using TTimer in my software and it supposed to run forever 24/7. Nowhere in my software is that timer disabled or stopped from running. It's main function is to update table's value. It is started as soon as the software is run and from that point on TTimer should not stop. However, after running for over a month, that TTimer mysteriously stops running. The software is run on Windows 7 and the software is developed on Delphi 2010 XE. I've searched my code to see what might be causing it, but I can't figure out what.

 Timer1.Enabled:=true;

That's how the timer is started.

UPDATE: After doing some investigation, I found out that the TTimer never stopped, but there is another issue. My TStringGrid table on a TForm just simply don't show any values being updated. Further, I also found out that my TList List I am using to store list of data item is being destroyed somehow that the list becomes empty. But the data items in the list are not deleted anywhere in the code once they are loaded only when the program is started.

Everytime I update the TStringGrid on the form, I run through my TList items from 0 to count-1 location. So, if there is no item in the TList in the memory, my code simply skips over the display part and thus nothing gets updated on the TStringGrid.

Something like:

If (List.count>0) then 
begin
//Display values in TStringGrid; 
end;

But while the software is still running, I was able to reload my list of items from a file back into TList list and my software started to work like it supposed.

I hate to say the ugliest word programmers hate the most. I am afraid I may have a memory leak. Anyone think so?

Any help will be greatly appreciated. Thanks.

Upvotes: 1

Views: 3329

Answers (4)

Monde Amazigh
Monde Amazigh

Reputation: 1

You should free memory allocated after the job is done:

Timer1.FreeOnRelease() or .Free();

Upvotes: 0

Ken White
Ken White

Reputation: 125669

I'm not sure why it's stopping after a month; I'd suspect (as Erik said) you have something using GetTickCount() that's failing after the wraparound at ~49 days.

As a general rule, though, it's better to stop/start the timer to prevent a delay from causing a timer message to be dropped:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  try
    // Do whatever on timer event firing
  finally
    Timer1.Enabled := True;
  end;
end;

You might try this instead of just allowing it to run constantly; if it is a bug in the TTimer code (I don't see anything after a quick scan of XE's TTimer implementation), stopping and starting may reset things to prevent the failure.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612914

TTimer is just a wrapper around the Windows SetTimer() API which I believe will run forever.

I suspect that the timer still runs, but the event handler that it fires is failing to operate as desired.

Upvotes: 1

Erik
Erik

Reputation: 91270

I bet it stops 49 days after reboot. When Windows GetTickCount wraps around. Sure you're not doing a check that would fail based on this?

Upvotes: 5

Related Questions