【发布时间】:2012-04-04 15:07:00
【问题描述】:
我正在生成一个文件流并将其包装在缓冲流阅读器中。然后我用读取线一次消耗一条流。在 X 行/字节数之后,我遇到了堆栈溢出异常。递归调用方法似乎不是问题,因为它可以毫无问题地处理较小的文件。我希望我在这里忽略了一些简单的事情。在这里发布整个 sn-p 有很多逻辑,但这是要点......
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.
}
【问题讨论】:
-
您没有向我们提供导致问题的行。在
InitializeRecord、LineReader或VerifyLine内部的某个地方存在您的问题。 -
我为抛出异常的 LineReader 方法进行了编辑。
-
我不确定我能不能把所有的代码都放在这里。这是超过500行。 VerifyLine 方法有条件地处理从 LineReader 传入的行,并有条件地调用其他四个生成输出变化的方法(标题、细节、子预告片和预告片)。所有行读取都发生在 LineReader 方法中,该方法工作正常,并在 5MB 以下的较小文件上返回预期结果。但这也是 LineReader 方法中 line = reader.ReadLine 上较大文件发生错误的地方。
-
如果您在调试器下运行它,您将能够在它崩溃时检查堆栈跟踪;它应该告诉你你不需要的递归来自哪里。
-
是的,我忘记了调试器中的调用堆栈窗口...我检查了堆栈跟踪,它在堆栈上的内存不足,如果到达文件末尾之前通过方法重新诅咒流式传输更大的文件。
标签: c# stack-overflow streamreader