2013-04-03 53 views
1

當我填充橢圓不僅僅是一個橢圓,而是多個橢圓時,我得到一個stackoverflow異常。StackOverflow在GraphicsCreator()的異常。FillEllipse

我不認爲這是圖形創建者的問題。但我想不通爲什麼調試器在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中

單個對象
+0

你嘗試逐步執行代碼,看看發生了什麼? – djs

+0

我無法逐步完成700次代碼 –

+1

我想你可以在那之前發現問題。 – djs

回答

0

這是因爲它試圖調用FillEllipse,實際上堆棧已經用完。

當然,堆棧溢出必須是由您的邏輯中的缺陷引起的,這會導致您的createPath方法被遞歸調用(可能)無限或太深,無法容納所有需要的激活幀。

0

使用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]; 
     } 
    }