Delphi Tips




How to find errors in code.


If from a start of the application or from some known call an error occur determinatively, but to find it the same line of code should be passed many times (e.g. in loop or in recursive call), it is possible to simplify such error searching (in Delphi5 or higher). Set a break point in the line which is executed many times before the error is occur, and change breakpoint properties and set a counter to some big value (e.g. 9999). To do so, press right mouse button on a red circle, marking breakpoint, select in pop-up menu item Breakpoint properties, then go to the field Pass count and enter a number there.

Run the application. When the error is occur, do not reset the application. Istead, find a breakpointed line and move mouse cursor over red dot indicating it. Delphi debugger will pop up a tooltip with counter value showing how much times this line were executed before the error was occur. Now, set this value as new value for break point counter, run the application again, and it is stopped just before an error occur (bot not yet occur). It is now possible to debug step by step and find the error. Certainly, this method can not be applied to undeterminated errors.




If some variable after assigning a desired value, some time later become assigned to another (incorrect) value, and you can not detect which code did this undesireable change, do the following:
- set a break point at the place where a problem variable has correct value;
- add the variable to the Watch list (it is desirable to provide context-independant fully qualified variable name, e.g., Form1.MyArray[4,6], or at all to determine the variable address in the memory and write something like PInteger($123456)^);
- click a line with the value in the Watch list with right mouse button and select pop up menu item Break when changed (line will become red).
- continue execution (F9). At the place where the variable is changed, the debugger stops the execution and you can analyse a stack and understand which your code and in which surcumstances currupted it.




Starting from version 5, Delphi has new power debugger tools to stop on a wide range of conditions, which is very convenient sometimes. But this slow down application speed and from the ancient time there is exist more convenient and efficient way to do this. Just write:

   if ( some_condition ) then
    asm
     int 3
    end;

When a condition is satisfied application is stopped (in the next line of source code) and can be debugged step by step.


Copyright (c)2008, BDSLib.com