简体   繁体   English

StreamReader.ReadLine抛出一个奇怪的ArgumentOutOfRangeException

[英]StreamReader.ReadLine throwing a strange ArgumentOutOfRangeException

I'm using a StreamReader on a large text-file (~300 MB) and calling ReadLine() in a loop eventually throws a weird ArgumentOutOfRangeException : 我在大型文本文件(〜300 MB)上使用StreamReader ,并在循环中调用ReadLine()最终抛出了一个奇怪的ArgumentOutOfRangeException

Index and count must refer to a location within the buffer.
Parameter name: bytes

The stacktrace is: stacktrace是:

at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount,
Char[] chars, Int32 charIndex, Boolean flush)
at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount,
Char[] chars, Int32 charIndex)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadLine()
....

Is this some weird memory error? 这是一些奇怪的内存错误吗? It doesn't throw OutOfMemoryException so I'm a bit lost. 它不会抛出OutOfMemoryException所以我有点迷路。 It happens around the 2000th line. 它发生在第2000行附近。 By that time, even if it continues with a successful ReadLine(), it starts throwing weird exceptions elsewhere. 到那时,即使它继续成功执行ReadLine(),它也会开始在其他地方引发奇怪的异常。 So there's a probably memory leak somewhere, but is there anything specific to ReadLine() or StreamReader to look at? 因此,某处可能存在内存泄漏,但是要查看ReadLine()StreamReader特定内容吗?

Each line in my text file is exactly 24 characters long. 我的文本文件中的每一行正好是24个字符长。

If the problem is specific to StremReader.ReadLine() it may be: 如果问题特定于StremReader.ReadLine(),则可能是:

  • the number of characters in the next line is larger than Int32.MaxValue 下一行中的字符数大于Int32.MaxValue
  • the buffer you passed to StremReader constructor is too small 您传递给StremReader构造函数的缓冲区太小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM