【问题标题】:Streamreader and StackOverflowException [closed]Streamreader 和 StackOverflowException [关闭]
【发布时间】:2018-02-04 00:05:19
【问题描述】:

当分配我的StreamReader 读取文件时,我有一个StackOverflowException,我不知道为什么会发生这种情况:(

这里是异常详情:

$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      

代码如下:

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

【问题讨论】:

  • 你能发布异常堆栈跟踪吗?
  • 您要读取的文件有多大?
  • 该文件实际上可能只有几行,大约 1kb 左右
  • 你能发布 importInfo.FullName 属性代码吗?可能会进入某种递归?
  • 我们需要查看ParseLineIntoToken()ActOnToken() 方法。

标签: c# .net exception stack-overflow


【解决方案1】:

我不知道这是否与你的问题有关,但是为StreamReader使用静态变量意味着如果2个线程同时调用CompileAndRun,可能会出现问题。

另外,StreamReader imments IDisposable,所以你应该在它上面调用Dispose 或使用using 这样的块:

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

另外,不应该这行吗:

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

是:

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

?否则,您不会在任何地方使用从文件中读取的行,因此读取文件毫无意义。

【讨论】:

  • 我不敢相信我从来没有发现过这个,谢谢! (是的,它有效。)
猜你喜欢
  • 2018-11-10
  • 1970-01-01
  • 2010-12-20
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多