Integer overflow problems

Posted on 2006-06-22
Last Modified: 2012-05-05

I have a project with many calculations inside some timers.
After some seconds (~ 90000, almost a day) it pop up a message sayint "integer overflow"
and i have to terminate the program.

I've turned on Overflow checks in Options ---> Compiler.
I've put this line at the beggining : {$OVERFLOWCHECKS ON}

In EVERY calculation i've put :

Except on Exception do
  ErLog.Add('Error in "Section 1"');

(variables ErLog:TStringList; ErPath:String;)

BUT the program does NOT create an error log. It gives the error messages again and again.
It bypass the try ...except as it seems....

I tried to run the program from delphi with timer interval 1msec and let it run for 2million times !
No error. I can reproduce the error and i dont know how to find it.

Can you help ?
Thanks in advance.
Question by:CodedK
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
LVL 28

Assisted Solution

2266180 earned 75 total points
ID: 16966744
I would blame it on the "sizes"/ if you have D7+, then compile your project and check your hint messages. everywhere where you see that the sizes missmatch and that it was made larger: make sure you change the type so that the warning goes away. (sorry, but I cannot rember the hint message and can't seem to be able to reproduce it right away)

I also found an interesting article:
LVL 10

Accepted Solution

atul_parmar earned 75 total points
ID: 16966777
The error means that you are assining a large value (what can not be stored in integer) to integer variable.

just change the data type from integer to longint; or int64
LVL 16

Author Comment

ID: 16968399
Thanks ciuly :) . There are no hints !!!
(only a depricated command at some point BUT i've used it since the version 1
of this project without any problems).
Before i post this question i was reading that article :)
I've placed everywhere "*1.0" in multiplications. I did everything the article says.

Atul_parmar thank you, of course i know that, i could replace int with int64 but this would make the app,
use TOO much memory.
And i dont assign big values (2 or MAX 3 digits) and in every tick of the timer they turn to zero.

I'm trying to reproduce the problem by running the program from Delphi 7,
I've tried with interval '1 sec' (as it is) and let it run for 2 days.
I've tried with interval '1 msec' and let it run for 15 hours (15.000 hours of calculations in real life)
BUt there was no problem.

When i run the program from windows then at some point ~80.000 - 90.000 seconds (1 day)
there is that integer flow message popping up every second.

The problem is WHY it bypass the TRY... EXCEPT ?

Thanks for your help.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 16

Author Comment

ID: 16968411
Ciuly can you explain more about the sizes.. What do you mean by size mismatch?
LVL 28

Expert Comment

ID: 16968468
well .. when you have operands of some type and result is anotehr type.


byte*byte = word
but if you do byte = byte*byte you have a type size issue as byte*byte will not fit in a byte.

until we figure it out, install madhis madexcept. compile with debug info and all that, and that catch the exception like that (maybe it's not an exception but an error, in which case a simple catch exception deosn't help)

let the app run until it booms.

I'll try to think of other reasons that could cause an integer overflow in the OS as this is probably not delphi issue.
LVL 10

Expert Comment

ID: 16968491
you can also try with unsigned if you don't require -ve numbers

Assisted Solution

Scay7 earned 75 total points
ID: 16975609
After some seconds (~ 90000, almost a day) it pop up a message sayint "integer overflow"

This part where it works out the seconds, mabey you assigned a value as byte rather then int64 or longint
mabey its those counting of seconds thats causing the error :P

Just a thought...

Peace Scay7
LVL 11

Assisted Solution

ZhaawZ earned 75 total points
ID: 16978050
>> just change the data type from integer to longint; or int64
atul_parmar, I'm not sure about "new" Delphi versions, but in D6 longint and integer were synonyms (i.e., both are 32bit signed integers, with same range –2147483648..2147483647)

About sizes (i.e., assigning 16bit value to 8bit) - shouldn't it ignore waste bits instead of throwing an error?

CodedK, I'm not sure if it helps but maybe you should remove that "on Exception do"? It would look like this:

  // do something
  // save error log

Or try using "on EIntOverflow do" instead of "on Exception do".
LVL 11

Expert Comment

ID: 16978067
Another thing you could try is using 'try .. except .. end' for whole app instead of using it only for calculations (just in case if u get overflow somewhere else).

Project -> View Source -> ...

  Application.CreateForm(TForm1, Form1);
  // save log
LVL 11

Expert Comment

ID: 16978092
And one more thing - do not use "compile and run" for testing "try .. except" stuff. Run an app without involving Delphi (or disable debugger before running an app from Delphi).
LVL 16

Author Comment

ID: 16980076
Thanks Zhaawz.
I'll give it a try without on Exception to see if it can create the log.
LVL 16

Author Comment

ID: 17002330
Thank you all for your help and sorry for the delay.
I've searched thoroughly the code to find errors. I didnt found any...

So i've replaced all integers with Int64 and the error didnt occur...
This is half measure but at least the app is running fine.
Though i cant understand why it doesnt go in the try...except when the error occur.

I will split points equally and accept atul_parmar suggestion.
Thank you.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question