Solved

How to debug For loops?

Posted on 2016-10-09
3
33 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 36

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 36

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

18 Experts available now in Live!

Get 1:1 Help Now