2017-05-13 26 views
-3

我在運行時遇到了StackOverflowException,我確定我調用了太多的方法,只是無法確定發生這種情況的位置。當我運行程序時,異常發生在聲明變量structurePath變量。C#StackOverflowException,太多的方法調用

FolderContentManagement.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace VINA_BATCH.FFManagement 
{ 
class FolderContentManager : FileContentManager 
{ 
    public int currIndex = 0; 

    private VinaProcess.VProcess vproc = new VinaProcess.VProcess(); 
    private string structurePath = Path.Combine(Directory.GetCurrentDirectory(), "structures"); 
    private string structureExt = "*.pdbqt"; 
    private Dictionary<string, string> files = new Dictionary<string, string>(); 

    public FolderContentManager() { } 

    //Returns list of structures 
    public string[] GetStructuresPath() 
    { 
     return Directory.GetFiles(structurePath, structureExt); 
    } 


    public string[] GetStructureNames() 
    { 
     string[] structs = Directory.GetFiles(structurePath, structureExt); 

     for(int i = 0; i < structs.Length; i++) 
     { 
      structs[i] = Path.GetFileName(structs[i]); 
     } 

     return structs; 
    } 

    public string GetCurrentStructureName() 
    { 
     string currPath = this.GetCurrentStructurePath(); 
     return Path.GetFileName(currPath); 
    } 

    public string GetCurrentStructurePath() 
    { 
     string currPath = ""; 
     string[] paths = Directory.GetFiles(structurePath, structureExt); 
     for (int i = 0; i < paths.Length; i++) 
     { 
      if (i == currIndex) 
       currPath = paths[i]; 
     } 

     return currPath; 
    } 

    public string GetNextStructurePath(int index) 
    { 
     string[] names = GetStructureNames(); 
     string[] paths = GetStructuresPath(); 

     string nextPath = ""; 

     for (int i = 0; i < names.Length; i++) 
     { 
      if (i == index) 
       nextPath = paths[index + 1]; 
     } 

     return nextPath; 
    } 

    /* 
    public void CompilePathsFiles() 
    { 
     string workingPath = GetWorkingPath(); 
     string[] tempFiles = { GetCurrentStructureName(), findProtein(), "conf.txt", "log.txt" }; 

     for(int i = 0; i < tempFiles.Length; i++) 
     { 
      if (i == 0) 
       files.Add(structurePath, tempFiles[i]); 

      files.Add(workingPath, tempFiles[i]);     
     } 

     MessageBox.Show(files.ToString()); 
    } 
    */ 
    public void Move_RunRoutine() 
    { 
     /* 
      - After conf.txt change copy to vina folder 
      - Copy the rest of the working folder file to vina folder 
      - Copy the current *.pdbqt file to the vina folder 
     */ 



     string destination = vproc.GetVinaPath(); 

    } 

    public void Move_OutRoutine() 

    { 
     /* 
      - Once an iteration is done move the contents of the vina folder to out folder with the name of the *.pdbqt file 
     */ 
    } 

} 
} 

GetCurrentStructurePath()正在從像這樣另一類調用。這是此類唯一參考FolderContentManagement

contents[1] = String.Format("ligand = {0}", fcm.GetCurrentStructurePath()); 

任何幫助將不勝感激。

+2

我建議當你在_GetCurrentStructurePath_ – Steve

+0

中找到搜索到的文件時添加一個_break_確切的說,這是引發異常的行'private string structurePath = Path.Combine(Directory。GetCurrentDirectory(),「structures」);' –

+0

當你有一個無限遞歸函數時,Stackoverflows通常會發生。 – Krythic

回答

0

我敢肯定,我打電話太多methods`:

這不是爲什麼StackOverflowException被拋出。應用程序可以調用多少個方法沒有限制。

這就是爲什麼StackOverflowException被拋出;引用自MSDN

執行堆棧溢出時引發的異常,因爲它包含過多的嵌套方法調用。 StackOverflowException拋出執行堆棧溢出錯誤,通常在遇到非常深或無限遞歸的情況下。

你說:

當我運行structurePath變量聲明時發生異常的程序。

我不這麼認爲。這裏是一個快速測試,你可以做到這證明該問題在別處:

public class Test 
{ 
    public string structurePath = Path.Combine(Directory.GetCurrentDirectory(), "structures"); 
} 

var test = new Test(); 
var path = test.structurePath; 

要解決

開始通過看

  1. 任何遞歸的方法,你可能有,並確保他們有終止條件
  2. 檢查你的屬性,並確保setter不是這樣調用getter:

    private int age; 
    public int Age 
    { 
        get { return this.age; } 
        // This is an easy mistake. It should be this.age (small a) 
        set { this.Age = value; } 
    } 
    

this回答有關疑難解答更多的想法。