C# GraphicsCreator()处的StackOverflow异常。FillEllipse
当我填充椭圆时,我得到一个stackoverflow异常,不仅仅是一个椭圆,而是许多椭圆 我不认为这是图形创建者的问题。但是我不明白为什么调试器在C# GraphicsCreator()处的StackOverflow异常。FillEllipse,c#,stack-overflow,C#,Stack Overflow,当我填充椭圆时,我得到一个stackoverflow异常,不仅仅是一个椭圆,而是许多椭圆 我不认为这是图形创建者的问题。但是我不明白为什么调试器在FillEllipse命令中指向stackoverflow异常 public void createPath(Stance currentStance) { if(toSort.Count > 0) { toSort.Remove(currentStance);
FillEllipse
命令中指向stackoverflow异常
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弹出一个对象,这是因为试图调用FillEllipse时堆栈实际上耗尽了
当然,堆栈溢出必须由逻辑中的缺陷引起,该缺陷导致递归调用
createPath
方法(可能)无限期调用,或者调用太深,堆栈无法容纳所有需要的激活帧。使用while循环而不是递归,以避免加载堆栈
下面是您的代码修改,可能会起作用:
public void createPath(Stance stance)
{
var currentStance = stance;
while(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());
currentStance = (Stance)toSort[0];
}
}
您是否尝试单步执行代码以查看发生了什么情况?我无法单步执行代码700次我认为您在此之前能够发现问题。不,我无法单步执行代码。对于少量的迭代,它工作正常,但当它有大量的迭代时,会出现以下错误:/