C#StackOverflowException,方法调用太多

时间:2017-05-13 17:00:19

标签: c# stack-overflow

我在运行时遇到StackOverflowException,我确定我调用了太多方法,只是无法确定这种情况发生的位置。当我运行程序时,在声明 structurePath 变量时会发生异常。

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());

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

  

我确信我调用的方法太多了:

这不是抛出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; } 
    }
    
  3. 有关故障排除的更多建议,请参阅this答案。