Solved

How to debug For loops?

Posted on 2016-10-09
3
52 Views
Last Modified: 2016-10-10
I use a lot of For loops and when I need to debug what is going on - step-by-step execution within the for loop,  I use break-point like this:

For i:=a to b do
begin
  ...
  if i=541 then
    sleep(0); <- this is the line with (F5) break-point to so I can analyze execution when i = 541
  ...
end;

Is there any other way, it is annoying when I need to add the code and breakpoint and recompile and run again.
I see there are options on Breakpoint Properties with Condition, Thread, Pass count, Group values.
I tried to set Pass count to 541 or Condition to i=541, but nothing worked on simple break-point on any line, without sleep(0);

Is there any easy way to do this?
0
Comment
Question by:Delphi_developer
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 41836404
set the breakpoint
right click on the red breakpoint ball
click breakpoint properties
and set the condition to i=54
breakpoint properties
writing to a text file can also help

it's also possible the optimizer removes code which does nothing
it usually gives a hint about that

and ... if you have a file open with the same name (thus, in another directory)
the debugger can get confused on which file to use for displaying the debug lines
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 500 total points
ID: 41836406
i'd add some logging

var mLogFile: Text;

procedure OpenLog(aFileName: string);
begin
  AssignFile(mLogFile, aFileName);
  if FileExists(aFileName) then 
    Append(mLogFile)  
  else 
    Rewrite(mLogfile);
end;

procedure AddLog(Msg: string);
begin
  WriteLn(mLogFile, FormatDateTime('yyyymmdd:hh:nn:ss.zzz', Now) + ' - ' + Msg);
  Flush(mLogFile);
end;

procedure CloseLog;
begin
  CloseFile(mLogFile);
end;

procedure yourproc;
begin
  OpenLog('loop.txt');
  AddLog('a = ' + IntToStr(a));
  AddLog('b = ' + IntToStr(b));
  For i:=a to b do
  begin
    ...
      if i=541 then
      begin
        AddLog('i=541 > the nasty line');
        // other nasty lines of code 
      end;
  end;

Open in new window

0
 

Author Comment

by:Delphi_developer
ID: 41836602
Well, now the Condition works! Was trying the same thing yesterday, but didn't work. I guess it was one of those days, Sunday.
Thank you also for quick logging example.
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

825 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