Solved

How to debug For loops?

Posted on 2016-10-09
3
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 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