detecting recursive loops

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

detecting recursive loops

Zaaphod
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?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: detecting recursive loops

Mark Morgan Lloyd-5
On 05/05/18 21:15, James Richters wrote:
> 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]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: detecting recursive loops

Free Pascal - General mailing list
In reply to this post by Zaaphod
> 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.



--
Sent from: http://free-pascal-general.1045716.n5.nabble.com/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal