C# StreamReader system.stackoverlow异常

C# StreamReader system.stackoverlow异常,c#,stack-overflow,streamreader,C#,Stack Overflow,Streamreader,我正在生成一个文件流,并将其包装到一个缓冲流读取器中。然后,我使用readline一次消耗一行流。在X行/字节数之后,我遇到堆栈溢出异常。递归调用方法似乎不是问题,因为它处理较小的文件时不会出现问题。我希望我忽略了一些简单的事情。在这里发布整个片段有很多逻辑,但这是要点 Instantiates a static stream reader // { using (FileStream fs = File.Open(filename, FileMode.Open, FileAcc

我正在生成一个文件流,并将其包装到一个缓冲流读取器中。然后,我使用readline一次消耗一行流。在X行/字节数之后,我遇到堆栈溢出异常。递归调用方法似乎不是问题,因为它处理较小的文件时不会出现问题。我希望我忽略了一些简单的事情。在这里发布整个片段有很多逻辑,但这是要点

Instantiates a static stream reader //
{    
    using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read,
    FileShare.Read))
    using (BufferedStream bs = new BufferedStream(fs))
    using (reader = new StreamReader(bs))

     InitializeRecord(reader) // passes reader in
}

InitializeRecord(StreamReader reader)
{  
    //Makes some determinations whether to take in the first line or skip to first    header record... This is working fine. Initializes first line = reader.ReadLine()
    // Calls the first method to generate the header output which in turns calls the LineReader Method to consume the next line.
}

LineReader()
{ // Main loop for iterating over lines where stackoverflow occurs
    while (!reader.EndOfStream)
    {
        string prev_line = line;
        line = reader.ReadLine(); // StackOverFlow occurs here only on larger files / # of bytes read        
        VerifyLine(line,prev_line);
    }
}

VerifyLine(string line)
{ 
    // Does some checking on the line and calls output methods for each record type which in turn calls LineReader which LineReader exits when the endofstream is reached. 
    //But is blowing up prior to reaching the end of the stream.  By writing the lines out to disk as it iterates it writes a replica of the stream perfectly until the stack overflow occurs. 
    //This is only the difference of anything greater than a 5 MB file. Some of these records are hitting 9 million characters. I tried increasing the buffer size without luck.
}
递归地调用一个方法似乎没有什么问题 处理较小的文件而不会出现问题


但你是说它会在更大的文件上爆炸,对吗?对我来说,这听起来像是递归有问题。是否仍然可以在没有递归的情况下执行操作?我想在verifyLine方法中看到更多代码

你没有给我们造成问题的线路。在
InitializeRecord
LineReader
VerifyLine
中的某个地方存在您的问题。我编辑了引发异常的LineReader方法。我不确定是否可以将所有代码都放在这里。这是一个500多行的系统。VerifyLine方法有条件地处理从LineReader传入的行,并有条件地调用其他四个方法,这些方法生成输出的变体(header、detail、sub-trail和trail)。所有的行读取都发生在LineReader方法中,该方法工作良好,并在5MB以下的较小文件上返回预期结果。但这也是LineReader方法中line=reader.ReadLine上的较大文件出现错误的地方;它应该会告诉你你不想要的递归是从哪里来的。是的,我忘记了调试器中的调用堆栈窗口。。。我检查了堆栈跟踪,发现堆栈内存不足,在if到达较大文件的文件流末尾之前,通过方法重新诅咒。是的,我没有任何无限循环,这就是为什么我不认为这是递归。但是根据上面的评论,我忘记了调试器中的调用堆栈窗口。在更大的文件上访问endofstream之前,通过方法重新诅咒内存不足。