Search code examples
c#.netexceptionstack-overflow

Streamreader and StackOverflowException


I have a StackOverflowException when assigning my StreamReader to read a file, I don't know why this is occurring :(

Here is the exception details:

$exception  {"Exception of type 'System.StackOverflowException' was thrown."}   System.StackOverflowException
+       Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
        HResult -2147023895 int
        HelpLink    null    string
+       InnerException  null    System.Exception
        Message "Exception of type 'System.StackOverflowException' was thrown." string
        Source  null    string
        StackTrace  null    string
        TargetSite  null    System.Reflection.MethodBase
+       Static members      
+       Non-Public members      

Here is the code:

class Compile_Import
{
    public static string getargs = null;
    private static StreamReader sr;
    public static void CompileAndRun(FileInfo importInfo, string args)
    {
        getargs = args;
        if(importInfo.Extension == ".cbe" && importInfo.Exists)
        {
            sr = new StreamReader(importInfo.FullName); //Where my problem occurs.
            string curlinecont = null;
            while((curlinecont = sr.ReadLine()) != null)
            {
                string RawToken = 
             Parser.Parse.ParseLineIntoToken(Run.Stats.CurrentLineContents);
                Token_Management.Process_Token.ActOnToken(RawToken);
            }
        }
    }

Solution

  • I don't know if this is related to your issue, but using a static variable for StreamReader means that if 2 threads call CompileAndRun at the same time, there will likely be problems.

    Also, StreamReader imlements IDisposable, so you should call Dispose on it or use a using block like this:

    class Compile_Import
    {
        public static string getargs = null;
        public static void CompileAndRun(FileInfo importInfo, string args)
        {
            getargs = args;
            if(importInfo.Extension == ".cbe" && importInfo.Exists)
            {
                using (StreamReader sr = new StreamReader(importInfo.FullName)) //Where my problem occurs.
                {
                    string curlinecont = null;
                    while((curlinecont = sr.ReadLine()) != null)
                    {
                        string RawToken = 
                     Parser.Parse.ParseLineIntoToken(Run.Stats.CurrentLineContents);
                        Token_Management.Process_Token.ActOnToken(RawToken);
                    }
                }
            }
        }
    

    In addition, shouldn't this line:

    string RawToken = 
        Parser.Parse.ParseLineIntoToken(Run.Stats.CurrentLineContents);
    

    be:

    string RawToken = 
        Parser.Parse.ParseLineIntoToken(curlinecont);
    

    ? Otherwise, you are not using the line that is read from the file anywhere, so it makes reading the file pointless.