Intermittent Stack Overflow Exception

I have a script designed to stress test the Physics class Raycasting functions. Initially, the script works fine and each time the test is run gives a small framerate hitch but otherwise works fine. However, after a few times running it, I get a Stack Overflow Exception. I’m thinking this is a garbage collection issue but I’m not sure where the leak is or even if it can be fixed with C#'s automatic collection. Can anyone tell me what’s wrong with the code, or how to optimize it more?

public class RaycastTest : MonoBehaviour
{
    public Transform target;
    private RaycastHit hit;
    private int lm;
    private int iterations = 0;
    private float radius = 0.5f;
    private float height = 2f;
    private float offset = Mathf.Sqrt(2f) + 0.05f;
    private Vector3 upper;
    private Vector3 lower;
    private Vector3 waypoint;
    private Vector3 normal;

    void Start()
    {
        lm = 1 << 8;
        lm = ~lm;
        upper = new Vector3(0, height * 0.5f - radius, 0);
        lower = new Vector3(0, -height * 0.5f + radius, 0);
        PathTest(transform.position, transform.forward);
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(100, 50, 200, 80), "New Path"))
        {
            iterations = 0;
            PathTest(transform.position, transform.forward);
        }
    }

    public void PathTest(Vector3 initial, Vector3 norm)
    {
        for (int i = 0; i < 16; i++)
        {
            Physics.CapsuleCast(initial + upper, initial + lower, radius, Rotate(norm, Random.Range(-90, 90)), out hit, 200f, lm);
            if (!Physics.CheckCapsule(hit.point + hit.normal * (radius + .01f), hit.point + new Vector3(0, height - 2 * radius, 0) + hit.normal * (radius + .01f), radius, lm))
            {
                waypoint = (hit.point + upper) + hit.normal * radius * offset;
                normal = hit.normal;
                iterations++;
                if (!Physics.CapsuleCast(waypoint + upper, waypoint + lower, radius, target.transform.position - waypoint, Vector3.Distance(target.transform.position, waypoint), lm))
                {
                    //Debug.Log("Target Found | Iterations: " + iterations.ToString());
                    //return;
                }
            }
        }
        if (iterations > 499)
        {
            Debug.Log("Iterations: " + iterations.ToString());
            return;
        }
        PathTest(waypoint, normal);
        return;
    }
}

Garbage collection is heap, not stack, so the issue is not Garbage collection. The #1 culprit for my personal stack overflow issues is recursion. PathTest() calls itself on on line 55. You bail out before you recurse if ‘iterations’ is greater than 499, but there is a pathway through your code that does not increment iterations. That is if the Physics.CheckCapsule() on line 38 is succeeding, your code does not increment iterations and therefore PathTest() will continue to call itself until the stack blows up.

I don’t fully understand your setup so I’m am unsure of the fix. But if it make sense, pulling the ‘iterations++’ outside of the Physics.CheckCapsule() should fix the stack overflow problem.