5

When an indefinite loop occurs in Delphi, the debugger will not even give me a stack trace when I hit the stop button. If I have a suspicion of where the program is stalling, I can put a breakpoint and it will stop if that is the correct indefinite loop.

Here is a sample program to deliberately cause an indefinite loop:

procedure TForm1.btnDebugInfiniteLoopClick(Sender: TObject);
var I: Integer;
begin
    I:=0;
    while I<100 do begin
        I:=1+1;
        if I>64 then I:=I div 2;
    end;
end;

When stopped, I get something that looks like:

ntdll.RtlUserThreadStart:
776301B4 89442404         mov [esp+$04],eax
776301B8 895C2408         mov [esp+$08],ebx
776301BC E9E99C0200       jmp $77659eaa
776301C1 8DA42400000000   lea esp,[esp+$0000]
776301C8 8DA42400000000   lea esp,[esp+$0000]
776301CF 90               nop 
ntdll.KiFastSystemCall:
776301D0 8BD4             mov edx,esp

...

As I single step (F7), it single steps a few lines, then locks up until I hit break again, at which point I get the same result.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
Robert Richter
  • 278
  • 2
  • 9
  • You don't indicate which version of Delphi you're using in this or your previous question. Since this may be version specific, you may want to add a tag that indicates that version as well as general `delphi`. – Ken White Oct 24 '12 at 20:37
  • Delphi XE3 is the version begin used. – Robert Richter Oct 24 '12 at 20:40
  • If you set a breakpoint in the suspect loop, you will see if program execution halts there. – GolezTrol Oct 24 '12 at 20:48
  • 8
    When you start single-stepping, are you sure you're stepping in the right thread? Delphi doesn't know which thread you want, so it just picks one. Make sure you select the main thread, and *then* check the call stack. – Rob Kennedy Oct 24 '12 at 20:57
  • Thanks, Rob! That helped a lot. I didn't realize I could get a call stack this way. – Robert Richter Oct 24 '12 at 21:04
  • By the way, what is your question? – Rob Kennedy Oct 25 '12 at 00:36
  • 1
    My question is how to get the debugger to show me where the program stalled at. For some reason, even though I am only using one thread, there are four threads in the debugger. Rob Kennedy answered my question by pointing out that I must choose the correct thread and then I can see where the program stalled. What was happening is I was trying to single step in assembly hoping to get to my source but I was in the wrong thread (and one I never created). – Robert Richter Oct 25 '12 at 00:46
  • similar problem here: http://stackoverflow.com/questions/42066834/program-freezes-but-the-cpu-utulization-is-zero – Gabriel Feb 06 '17 at 11:41

2 Answers2

7

Answered in comments by Rob Kennedy. I must open a thread view from debug window to get a list of threads and choose the correct thread; at that point I can see where my program is indefinitely looping.

Robert Richter
  • 278
  • 2
  • 9
1

As alternative answer: considering you're using Delphi XE3, it comes bundled with the profiler: AQTime which will find things like this real real fast.

Pieter B
  • 1,874
  • 10
  • 22