Search code examples
c#stack-overflow

StackOverflow Exception at GraphicsCreator().FillEllipse


Im getting a stackoverflow exception when i'm filling the ellipse not just one ellipse but many ellipses.

I dont think it is a problem from the graphics creator. But i can't figure out why the debugger is pointing a stackoverflow exception at the FillEllipse command

    public void createPath(Stance currentStance)
    {

        if(toSort.Count > 0)
        {
            toSort.Remove(currentStance);
            counter++;
        }

        this.currentForm.FillEllipse(new SolidBrush(Color.Red),new Rectangle(currentStance.location.X-3, currentStance.location.Y - 3, 6 , 6));

        foreach(Stance subStance in currentStance.childStances)
        {
            double weight = level(currentStance, subStance, 1)+ currentStance.StanceWeight;
            if (subStance.StanceWeight == -99999999999999)
            {
                currentStance.dajkstrasChildren.Add(subStance);
                subStance.parentStance = currentStance;
                subStance.StanceWeight = weight;
                toSort.Add(subStance);
            }
            else
            {
                if(weight > subStance.StanceWeight)
                {
                    try
                    {
                        subStance.parentStance.dajkstrasChildren.Remove(subStance);
                    }
                    catch (NullReferenceException e)
                    {
                        Console.WriteLine("null reference");
                    }
                    subStance.parentStance = currentStance;
                    currentStance.dajkstrasChildren.Add(subStance);
                    subStance.StanceWeight = weight;

                }
            }
        }

        foreach(Stance subStance in currentStance.secondChildStances)
        {         
            double weight = level(currentStance, subStance, 1) + currentStance.StanceWeight;
            if (subStance.StanceWeight == -99999999999999)
            {
                currentStance.dajkstrasChildren.Add(subStance);
                subStance.parentStance = currentStance;
                toSort.Add(subStance);
                subStance.StanceWeight = weight;
            }
            else
            {
                if (weight > subStance.StanceWeight)
                {   
                    if(subStance.parentStance != null)
                    {
                        try
                        {
                            subStance.parentStance.dajkstrasChildren.Remove(subStance);
                            subStance.parentStance = currentStance;
                            currentStance.dajkstrasChildren.Add(subStance);
                        }
                        catch(NullReferenceException e)
                        {
                            Console.WriteLine("null reference");
                        }
                    }

                }
            }
        }

        toSort.Sort(new Stance());
        if(toSort.Count != 0)
        {
            createPath((Stance)toSort[0]);
        }
    }

it is a recursive method but it cant recurse to infinity as it always pops a single object from the toSort ArrayList


Solution

  • That's because it is in the attempt to make the call to FillEllipse that the stack actually runs out.

    Of course the stack overflow has to be caused by a flaw in your logic that causes your createPath method to be called recursively either (probably) indefinitely or too deeply for the stack to accommodate all the needed activation frames.