Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-05

detecting recursive loops

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I'm having an issue with one of my programs that I suspect is being caused by a recursive loop... in simplified form... something like this:

Procedure Proc1;
Begin
   Proc2;
End;
Procedure Proc2;
Begin
   Proc1;
End;

I ended up getting a runtime error and the report showed something like above, where I had a loop of the same 3 procedures calling each other over and over in sequence in a loop before the actual error. Unfortunately I didn't take down all the information because I thought I could make it happen again.. but I haven't had the runtime error again... however under certain conditions sequences that normally happen very fast become very slow and I suspect it somehow is getting into this recursive loop again.

It's a very large very complicated program and trying figure out where this is happening is becoming quite a challenge. I was wondering if there is some method of detecting these recursive loops, either during compilation, or is there some option that would force a runtime error if a loop of procedures happened.

Any ideas how to identify potential unintentional loops?

Reply by email

Mark Morgan Lloyd
to James Richters
James Richters wrote 5 lines
I'm having an issue with one of my programs that I suspect is being caused by a recursive loop... in simplified form... something like this: Procedure Proc1;Begin Proc2;End;Procedure Proc2;Begin Proc1;End; I ended up getting a runtime error and the report showed something like above, where I had a loop of the same 3 procedures calling each other over and over in sequence in a loop before the actual error. Unfortunately I didn't take down all the information because I thought I could make it happen again.. but I haven't had the runtime error again... however under certain conditions sequences that normally happen very fast become very slow and I suspect it somehow is getting into this recursive loop again. It's a very large very complicated program and trying figure out where this is happening is becoming quite a challenge. I was wondering if there is some method of detecting these recursive loops, either during compilation, or is there some option that would force a runtime error if a loop of procedures happened. Any ideas how to identify potential unintentional loops?

I occasionally put entry/exit counters on functions that I suspect are going to be a problem, InterlockedIncrement() etc. You can obviously have your code check that as a verification of sanity, or pick up a haig value using a conditional breakpoint.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

leledumbo
to James Richters: I'm having an issue with one of my programs that I suspect is being caused by a recursive…
Any ideas how to identify potential unintentional loops?

That depends on your design. Is it intended that the 3 procedures can call each other, directly or indirectly? If no, then things are a lot easier. You can just put on the currently entered procedure name to a map shared by the 3 (or just make it global) and on start of each procedure, you check whether the current procedure is in the map already or not. If yes, then the current call stack has formed a recursion.

Reply by email