Solved

Integer overflow problems

Posted on 2006-06-22
12
2,485 Views
Last Modified: 2012-05-05
Hi.

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 :

TRY
....
.... CODE GOES HERE
....
....
Except on Exception do
 begin
  Timer1.Enabled:=False;
  Timer2.Enabled:=False;
  Timer3.Enabled:=False;
  Timer4.Enabled:=False;
  ErLog:=tStringList.Create;
  ErPath:=ExtractFileDir(Paramstr(0));
  ErLog.Add('Error in "Section 1"');
  ErLog.SaveToFile(ErPath+'\ErrorLog.txt');
  Application.Terminate;
 end;
end;

(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.
0
Comment
Question by:CodedK
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 28

Assisted Solution

by:2266180
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:
http://blogs.slcdug.org/rhordijk/archive/2004/09/12/539.aspx
0
 
LVL 10

Accepted Solution

by:
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
0
 
LVL 16

Author Comment

by:CodedK
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.
0
 
LVL 16

Author Comment

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

Expert Comment

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

ex:

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.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16968491
you can also try with unsigned if you don't require -ve numbers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Assisted Solution

by:Scay7
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
0
 
LVL 11

Assisted Solution

by:ZhaawZ
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:

try
  // do something
except
  // save error log
end;

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

Expert Comment

by:ZhaawZ
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 -> ...

begin
try
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
except
  // save log
end;
end.
0
 
LVL 11

Expert Comment

by:ZhaawZ
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).
0
 
LVL 16

Author Comment

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

Author Comment

by:CodedK
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Intraweb submit form as a POST request 4 263
IdTelnet1DataAvailable handler freezes the Application 4 68
delphi prevent click fast 2 191
Run video youtube webbrowse 10 58
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now