1

I am running a code in which there are too many nested method calls..On running it, gives a

stack overflow exception

due to too may nested method calls. How could I remove it??

Here's the code sample

public void searchProc(string path, string cPattern, string oPattern)
{
    var sqlFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories);
    string sqlObject = null;
    string content;


    Console.WriteLine("\nThe {0} TABLE is used in the following procedures :", oPattern);
    int cnt = 0;
    foreach (string currentFile in sqlFiles)
    {
        content = File.ReadAllText(currentFile);
        //      Console.WriteLine(currentFile);

        // condition checks whether the table that exists in a file is actually contained in the procedure
        if (Regex.IsMatch(content, oPattern, RegexOptions.IgnoreCase) && Regex.IsMatch(content, cPattern, RegexOptions.IgnoreCase))
        {
            cnt++;
            Match match = Regex.Match(content, cPattern, RegexOptions.IgnoreCase);
            sqlObject = match.Groups["proc_name"].Value;

            string[] split = sqlObject.Split('.');
            sqlObject = split[split.Count() - 1];

            sqlObject = sqlObject.Trim(charToTrim);
            using (StreamWriter sw = System.IO.File.AppendText(writePath))
            {
                sw.WriteLine(sqlObject);
            }

            searchProcInProc(path, cPattern, sqlObject);
        }
    }

    Console.WriteLine("Number of hits are : {0}", cnt);
}

public void searchProcInProc(string path, string cPattern, string oPattern)
{

    nestProc = new List<string>();
    sqlConnection.Open();
    cmd = new SqlCommand("SELECT CHILDPROC FROM PROC_DEPENDENCY_1_TBL WHERE PARENTPROC LIKE '%" + oPattern + "%'", sqlConnection);

    sqlReader = cmd.ExecuteReader();
    int cnt = 0;
    while (sqlReader.Read())
    {
        cnt++;
        nestProc.Add(sqlReader.GetString(0));
    }
   // Console.WriteLine("\n" + cnt);

    foreach (string s in nestProc)
    {
        using (StreamWriter sw = System.IO.File.AppendText(writePath))
        {
            sw.WriteLine("EXECUTED PROC " + s);
        }


    }
    string[] split;
    string temp;

    sqlConnection.Close();
    foreach (string s in nestProc)
    {
        temp = s;
        split = temp.Split('.');
        temp = split[split.Count() - 1];

        temp = s.Trim(charToTrim);
        searchProcInProc(path, cPattern, temp);
    }
}

The method SearchProcIn Proc is called again and again......The output contains multiple thousands of line

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Rishabh Gupta
  • 136
  • 1
  • 11
  • 3
    Currently we don't know what you're trying to achieve, or what your data is (although we do know you're building up SQL queries using values with string concatenation - bad idea, use parameterized SQL). Have you checked to see whether you're actually in an "infinite data loop" (e.g. results for query A require you to execute query B; results for query B require you to execute query A)? – Jon Skeet Aug 07 '14 at 08:04
  • Recursion loop - see keyword "loop of recurrence" :) seriously though, your question is missing what are you trying to achieve and schema for those tables. – Ondrej Svejdar Aug 07 '14 at 08:06
  • @JonSkeet actully my DB contains pairs of parent and child....I aam trying to find all the childs of parent.. from the root parent to the lowest child – Rishabh Gupta Aug 07 '14 at 08:06
  • @rishabh: Do that in your procedure instead of front-end. – Nikhil Agrawal Aug 07 '14 at 08:07
  • @JonSkeet I have to pass the whole model object of nested dependencies my view files in asp.net MVC framework because I need a complete tree view for it – Rishabh Gupta Aug 07 '14 at 08:09
  • you should construct the query in the database, either using stored procedure or a view, take a look at [this](http://stackoverflow.com/questions/207309/sql-query-for-parent-child-relationship) as reference – Yuliam Chandra Aug 07 '14 at 08:10
  • @rishabh - What if some object `A` has the parent `B`, and `B` has the parent `A`? – Corak Aug 07 '14 at 08:21
  • @rishabh: That really doesn't tell us anything about the data, or what diagnostics you've performed... have you logged the queries that are being executed, for example? You should probably try to detect cycles... – Jon Skeet Aug 07 '14 at 08:23
  • 1
    your SELECT in searchProcInProc could easily return the same values over and over if you have a pattern like "test", "test1", "test2"... LIKE '%test%' would return all 3... but without some example data it's hard to tell... can you eliminate this possibility? – fuchs777 Aug 07 '14 at 08:24
  • Doing it using recursion in SQL stored procedures can be tricky, it has quite a small maximum recursion limit. – Klors Aug 07 '14 at 08:30
  • @Corak yes that case is there – Rishabh Gupta Aug 07 '14 at 08:37
  • @fuchs777 that can't be the case – Rishabh Gupta Aug 07 '14 at 08:38
  • @rishabh - Okay, so you won't be able to get from root to leaf, if you're running in circles somewhere in between. - Maybe you just want to visit each node. So you could keep a list of visited nodes and if you visit a node you already visited, you know you don't need to go any deeper. – Corak Aug 07 '14 at 08:50
  • @Corak thanks for pointing out that case.... I found out just now that there can be the circular dependencies also... :) – Rishabh Gupta Aug 07 '14 at 09:01

1 Answers1

2

You can use this .NET function:

RuntimeHelpers.EnsureSufficientExecutionStack();

But I think you should refactor your code to avoid recursion.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Major
  • 5,948
  • 2
  • 45
  • 60