2012-04-04 11 views
0

私はファイルストリームを生成し、それをバッファリングされたストリームリーダーでラップしています。私はその後、ストリームを1行ずつ読み込み線で消費しています。 Xの行数/バイトの後、私はスタックのオーバーフローの例外を打つ。小さなファイルを問題なく処理するので、メソッドを再帰的に呼び出すことは問題ではないようです。私はちょうどここで簡単なものを見過ごしてほしいと思っています。StreamReader system.stackoverlow例外

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. 
} 
+0

問題の原因となった行は入力していません。 'InitializeRecord'、' LineReader'、または 'VerifyLine'のどこかに問題があります。 – user7116

+0

例外がスローされているLineReaderメソッドのために編集しました。 –

+0

「VerifyLine」はどうですか? –

答えて

1

それは再帰的に問題

ずに プロセスの小さなファイルとしてメソッドを呼び出すと、問題ではないようです...そこここに全体のスニペットを投稿する多くのロジックにありますが、これはその要旨であります

しかし、それはより大きいファイルに爆発すると言っていますか?私にとっては、再帰に問題があるように思えます。再帰なしで操作を実行するにはどうすればよいですか?私は、verifyLineメソッドでより多くのコードを見たいと思います。

+0

ええ、無限ループが起こっていないので、再帰ではないと思ったのです。しかし、上記のコメントに基づいて、私はデバッガの呼び出しスタックウィンドウを忘れていました。それは、大規模なファイルでendofstreamに達する前にメソッドを再抑制するメモリ不足です。 –