Solved

How to debug For loops?

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

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

14 Experts available now in Live!

Get 1:1 Help Now