1

楕円を 1 つだけでなく、多くの楕円で埋めようとすると、スタック オーバーフローの例外が発生します。

グラフィッククリエーターの問題ではないと思います。しかし、デバッガーがFillEllipseコマンドでスタックオーバーフロー例外を指している理由がわかりません

    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]);
        }
    }

これは再帰的なメソッドですが、toSort ArrayList から常に単一のオブジェクトをポップするため、無限に再帰することはできません

4

2 に答える 2

0

これは、スタックが実際に不足するのは、FillEllipse を呼び出そうとしているためです。

もちろん、スタック オーバーフローは、createPathメソッドが (おそらく) 無期限に再帰的に呼び出されるか、スタックが必要なすべてのアクティブ化フレームに対応するには深すぎて呼び出されるロジックの欠陥によって引き起こされる必要があります。

于 2013-04-03T18:14:43.980 に答える