Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: “Not enough timers available”


This question is answered.


Permlink Replies: 6 - Last Post: Jul 31, 2017 9:54 AM Last Post By: Remy Lebeau (Te...
Francisco Alvar...

Posts: 83
Registered: 11/10/06
“Not enough timers available”  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 9:49 AM
We have an application that is running 24/7 (Delphi 10.1 and Windows 7) and uses 4 TTimers to perform some tasks at several intervals.
A few weeks ago we started receiving the error “Not enough timers available” at random times.
I googled it and found this:
The VCL timer component reports this message whenever anything goes wrong with a timer
regardless of the nature of the problem: if you have the VCL source then look at the function
TTimer.UpdateTimer. The timer has a private field FWindowHandle which is a handle to the window
used to handle the timer messages. Whenever UpdateTimer is called (i.e whenever the timer is
enabled or its update rate is changed) UpdateTimer calls Windows.SetTimer(FWindowHandle, etc.... )
if this call fails then you will get a 'Too Many Timers' error regardless of the actual cause of the error.
This is a somewhat liberal assumption. When a colleague of mine had the same problem as yours
(random and seemingly illogical occurances of 'Too many timers'), we replaced the timer with a
modified version of the std TTimer which called GetLastError after SetTimer in TTimer.UpdateTimer.
Not surprisingly, the error returned was 'Invalid Window Handle'. Eventually the problem turned out
to be corruption of the Timer's window handle (FWindowHandle) by a memory overwriting bug.
I think I would be tempted to put a MemoryChanged breakpoint on FWindowHandle to start with.
Happy Hunting.
Martin Lafferty

Now, my questions are: How can we avoid this error without changing the code of TTimer? Is there
a 3rd party component that isn't based on this API?

Best Regards
Francisco Alvarado
Mark Marks

Posts: 269
Registered: 9/11/00
Re: “Not enough timers available”  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 10:00 AM   in response to: Francisco Alvar... in response to: Francisco Alvar...
Francisco Alvarado wrote:

'Invalid Window Handle'. Eventually the problem turned out+ +to be
corruption of the Timer's window handle (FWindowHandle) by a memory
overwriting bug.+ +I think I would be tempted to put a MemoryChanged
breakpoint on FWindowHandle to start with.+ Happy Hunting. +Martin
Lafferty+

Now, my questions are: How can we avoid this error without changing
the code of TTimer? Is there a 3rd party component that isn't based
on this API?

For me, the point is the problem is in your code, not the possibly
incorrect error message.

Maybe you are only attempting verify the error message is valid?
You can change the code of TTimer to get the actual error message and
then change it back.

Perhaps I am missing something.

My2c.
Francisco Alvar...

Posts: 83
Registered: 11/10/06
Re: “Not enough timers available”  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 11:14 AM   in response to: Mark Marks in response to: Mark Marks
For me, the point is the problem is in your code, not the possibly
incorrect error message.

Maybe you are only attempting verify the error message is valid?
You can change the code of TTimer to get the actual error message and
then change it back.

Perhaps I am missing something.

My2c.
The possible explanation I quoted is the only I found.
This application is running 24/7 in more than 70 desktop computers and it fails once in a while.
It was developed some years ago in Delphi 5 and later updated to D 2010. Now it's compiled in D 10.1
For years it worked flawlessly and in this month it has failed three times in five or six stations.
We reviewed the code and we are looking for other applications also running in the station.
We'll test the VrTimer component from TMS Software and wait to get more suggestions from this group.
Best Regards
Francisco Alvarado
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: “Not enough timers available”
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 1:32 PM   in response to: Francisco Alvar... in response to: Francisco Alvar...
Francisco Alvarado wrote:

How can we avoid this error without changing the code of TTimer?

Find and fix the bug that is corrupting the window handle to begin
with. This is not a bug with the TTimer itself.

Is there a 3rd party component that isn't based on this API?

SetTimer() is the primary timer API on Windows, but there are other
APIs, like timeSetEvent(), which is a threaded multimedia timer. And
yes, there are 3rd party components for that API. But if your timer
code is UI-based then switching to that API would not be worth it.

You could simply call SetTimer() directly, instead of using TTimer, and
do your own error handling.

--
Remy Lebeau (TeamB)
Francisco Alvar...

Posts: 83
Registered: 11/10/06
Re: “Not enough timers available”  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 3:13 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy:

Find and fix the bug that is corrupting the window handle to begin
with. This is not a bug with the TTimer itself.

How can the application corrupt the window handle?
I'm sorry if my question is naive or amateur, but I don't use any API calls that could mess with the handle of the main application.
Only queries to SQL-Server and capture some data to display. The application uses 5 timers to automatically display information, change colors of events based on their time opened and so on.

There are other applications running in the same station, can these corrupt the window handle the Timer uses?

Best Regards
Francisco Alvarado
Brian Hamilton ...

Posts: 556
Registered: 10/14/04
Re: “Not enough timers available”
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 5:16 PM   in response to: Francisco Alvar... in response to: Francisco Alvar...
I think the key is this :
"Eventually the problem turned out
to be corruption of the Timer's window handle (FWindowHandle) by a memory overwriting bug."
i.e there is some memory corruption going on, e.g writing outside an array boundary or a memory leak ?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: “Not enough timers available”
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 31, 2017 9:54 AM   in response to: Francisco Alvar... in response to: Francisco Alvar...
Francisco Alvarado wrote:

How can the application corrupt the window handle?

As you already stated in your original message:

Eventually the problem turned out to be corruption of the Timer's
window handle (FWindowHandle) by **a memory overwriting bug**.

Random memory was being overwritten, and that memory just HAPPENED to
be occupied by the timer's HWND at the time.

Do as Martin suggested:

I think I would be tempted to put a MemoryChanged breakpoint on
FWindowHandle to start with.

Start your app in the debugger and put a data breakpoint on the timer's
FWindowHandle member after the timer is created. If the HWND changes
value for any reason during the lifetime of the timer, the breakpoint
will trigger, and you can see what code is actually running in the
thread that is modifying the memory. The only things that should be
altering the FWindowHandle are the timer's constructor and destructor.
Anything else is a bug.

There are other applications running in the same station, can these
corrupt the window handle the Timer uses?

No. It has to be something inside your own app.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02