-3

I am getting a StackOverflowException during run time, I'm sure that I am calling too many methods, just can't pinpoint where this is happening. When I run the program the Exception occurs when the structurePath variable is declared.

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() is being called from another class like so. This is the only reference to FolderContentManagement that this class has.

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

Any help would be much appreciated.

4
  • 2
    I suggest to add a break when you find the file searched in GetCurrentStructurePath Commented May 13, 2017 at 17:04
  • To be exact this is the line that is raising the exception private string structurePath = Path.Combine(Directory.GetCurrentDirectory(), "structures"); Commented May 13, 2017 at 17:22
  • Stackoverflows usually occur when you have an infinitely recursive function. Commented May 13, 2017 at 17:39
  • 1
    Please be aware that a StackOverflowException is similar to the proverbial last drop in a glass that make it overflow. It may not be a problem with that drop, it may instead be a problem with whatever filled the glass beforehand. In any case, most likely you have a recursive method somewhere, so go hunt for it. Commented May 13, 2017 at 17:42

1 Answer 1

1

I'm sure that I am calling too many methods`:

That is not why a StackOverflowException is thrown. There is no limit on how many methods an application can call.

Here is why a StackOverflowException is thrown; quoted from MSDN:

The exception that is thrown when the execution stack overflows because it contains too many nested method calls. StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion.

You say:

When I run the program the Exception occurs when the structurePath variable is declared.

I do not think so. Here is a quick test you can do which proves the issue is elsewhere:

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

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

To Troubleshoot

Start by looking at

  1. any recursive methods you may have and make sure they have a terminating condition
  2. Check your properties and make sure the setter is not calling the getter like this:

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

See this answer for more ideas on troubleshooting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.