Compiling my Delphi application causes "Internal Error", what does this mean?
Compiler Messages Expert (Delphi 6.0 only)
Before I get started, I've written a Delphi 6.0 expert that provides additional help on Delphi compiler messages which is available here. This expert
will not resolve compiler message rather it will simply provide you with additional information which may be useful in resolving
the error/hint/warning message. The expert provides help on over 33 different kinds of compiler messages. Please link to this
page or others on this site and let people know that this expert is available.
Delphi 7.0 has this feature built-in and is available by selecting View | Additional Compiler Info from within the IDE.
Internal Errors Explained
Occasionally when compiling an application in Delphi compilation will halt with an error message that reads "Internal Error: X1234"
(this is just an example). This is indicative of a bug in the compiler and that it has encountered a situation where it can no longer
continue to compilation. Although you've encounted a compiler bug in nearly all cases it is possible to find an alternative
solution short of waiting for Borland to release a compiler patch/update.
What does the error message tell me?
While the error message seemingly means very little to the end user it may contain information useful to Borland
to help track down the problem. The message "Internal Error" indicates that the compiler has encountered a condition,
other than a syntax error, that it cannot successfully process and therefore compilation is aborted. The information following "Internal Error"
is usually one to three characters immediately followed by a number which indicates the compiler source file and line number within that file
where the error occurred. Because the error represents a file and line number there is no complete listing of all possible
internal error messages but typically they fall into one of two categories:
- Problems related to deleting old .DCU/.DCP files and rebuilding your application
- Problems related to coding constructs which the compiler fails to compile correctly and will require changes to your code in order to workaround
In almost every case there exists a plausible workaround for the error and many times it comes down to methodically eliminating the
possible causes to resolve the problem.
What to do when you encounter an "Internal Error"
- If the error occurs immediately after you have modified code in the editor go back to the spot where you made your changes and make a note of what was changed.
- If you can undo or comment out the change and recompile your application successfully it is possible that the programming construct that you were using exposes
a problem with the compiler so jump down to step 7. If not try, the next few steps to resolve your problem.
- Delete all of the .DCU and .DCP files associated with your project.
- Close your project completely using File | Close All, then reopen your project, this will clear the unit cache maintained in the IDE. Alternatively you can simply
close the IDE and restart.
- Another options is to try and rebuild your entire application using the Project | Build option so that the compiler will regenerate all of your DCUs.
- If the error is still present exit the IDE and try to compile your application using the command line version of the compiler (dcc32.exe) from a command prompt.
This will remove the unit caching of the IDE from the picture and could help to resolve the problem.
- If the problem still exists go back to the place where you last made modifications to your file and review the code.
Configuring Delphi to avoid possible Internal Errors
There are a few steps you can take to avoid situations where you have compiler generated .DCU/.DCP files that are out-of-date.
Create a single directory where all of your .DCP files (precompiled package files) are placed. For example, create a directory called C:\DCP and under
Tools | Enviroment Options select the Library tab and set DCP output directory to C:\DCP. This setting it will help ensure that the .DCP files the compiler generates are always up-to-date which is particularly useful when you move a package from one directory to another.
Similarly I suggest creating a .DCU directory which can easily be done on a per-project basis from the Project Options dialog by selecting Project | Options and clicking the
Directories/Conditionals tab and setting the Unit output directory. The key here is that you always want to be using the most up-to-date versions of your .DCU and .DCP files otherwise you may encounter internal errors which
are easily avoidable.
Identifying code that causes internal errors
Typically, most internal errors that are not related to out-of-date .DCU/.DCP files typically occur
as a result of syntax or constructs that are rather unusual or unexpected (as illustrated below) and can frequently be reproduced
with only a few lines of code. If this is the case you can try modifying the code in question
to do the same thing but in a different way. For example, if you are typecasting a value try
declaring a variable of the cast type and do an assignment first.
Like this:
begin
if Integer(b) = 100 then...
end;
var
a: Integer;
begin
a := b;
if a = 100 then...
end;
Here is an example of unexpected code which can be corrected by the developer to resolve the error:
var
A : Integer;
begin
{ Below the second cast of A to an In64 is unnecessary, removing it can avoid an Internal Error. }
if Int64(Int64(A))=0 then
end;
Techniques for Resolving Internal Errors
Here are some ways to try and resolve internal errors:
- If error seems to be on code contained within a while...do loop try using a for...do loop instead or vice versa.
- If it uses a nested function or procedure (a procedure/function contained within a procedure/function) try "unnesting" them.
- If it occurs on a typecast look for alternatives to typecasting like using a local variable of the type you need.
- If the problem occurs within a with statement try removing the with altogether.
- Try turning off compiler optimizations under Project | Options | Compiler (a rather last resort option) and remember if necessary you can use
the {$O-} compiler directive to turn optimizations off for small blocks of code and not the entire project. See the
documentation for usage information on this directive.
When all else fails
Typically there are many different ways to write any single piece of code which you can use
to your advantage when trying to resolve an internal error and while
this may not be the best solution it may help you to continue work on your
application. For example, you can change a "for" loop to a "while" loop or vise versa. Should this resolve the
problem it does not mean that either "while" loops or "for" loops are buggy and therefore should be avoided but
that perhaps the manner in which the code is constructed, and therefore compiled, has revealed a problem in the compiler.
If you've tried your code on the latest release of the compiler and it is still reproducible please create the
smallest possible test case to reproduce it and submit it to Borland. If
it is not reproducible on the latest version it is unlikely that we will go back and
update the version you are using so be creative in looking for ways around the problem!
Revision History
revision 1.12
date: 2002/01/12 04:19:41; author: strefethen; state: Exp; lines: +4 -4
- Minor grammtical fix
----------------------------
revision 1.11
date: 2002/01/03 00:53:14; author: strefethen; state: Exp; lines: +1 -1
- Updated meta tags
----------------------------
revision 1.10
date: 2002/01/02 17:42:48; author: strefethen; state: Exp; lines: +28 -15
- Clarified some confusing sentences
----------------------------
revision 1.9
date: 2001/12/11 22:40:08; author: strefethen; state: Exp; lines: +16 -11
- Further improvements to this page
----------------------------
revision 1.8
date: 2001/12/11 17:27:34; author: strefethen; state: Exp; lines: +17 -4
- Added more information to try and make this page clearer
----------------------------
revision 1.7
date: 2001/11/26 19:27:20; author: strefethen; state: Exp; lines: +9 -5
- Minor edits
----------------------------
revision 1.6
date: 2001/09/18 22:05:00; author: strefethen; state: Exp; lines: +6 -5
- Moved the information about the compiler message expert to the top of the file.
----------------------------
revision 1.5
date: 2001/09/06 17:59:10; author: strefethen; state: Exp; lines: +1 -0
Added page counter
----------------------------
revision 1.4
date: 2001/08/20 20:32:46; author: strefethen; state: Exp; lines: +5 -0
Added link to message expert.
----------------------------
revision 1.3
date: 2001/08/16 17:53:11; author: strefethen; state: Exp; lines: +21 -17
Improved the appearance of this page.
----------------------------
revision 1.2
date: 2001/07/31 20:50:25; author: strefethen; state: Exp; lines: +3 -0
- Added meta tags for search engines
----------------------------
revision 1.1
date: 2001/07/31 15:27:56; author: strefethen; state: Exp;
branches: 1.1.1;
Initial revision
----------------------------
revision 1.1.1.1
date: 2001/07/31 15:27:56; author: strefethen; state: Exp; lines: +0 -0
Initial checkin
Page hits: