Solved

How to debug For loops?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 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