.net 2.0下往流输出文本多了抛异常

前天在群里聊天的时候,喵喵提到.net 2.0平台下,有个控制台程序运行时间久了,呃,要说有多久可能一个月那种程度吧,崩了。而且是崩在Console.Write里面。说上网找了以后说是.net平台自己有问题。

第二天有空的时候,我自己试验了一下,确实很坑,我开了个文本文件(FileStream),然后以它创建一个StreamWriter,之后往里面写了三四个G的数据,写一半的时候就直接抛出了异常,异常信息和喵喵描述的一致。

这样说的话问题大概出在StreamWriter会用到的某些代码的实现里面?因为Console.Write的行为要说起来,也确实很像StreamWriter,下面的Stream是Standard output(标准输出,C语言里面写stdout)。这种坑感觉就像是编译器的bug一样,比如VC 6.0自带的标准库里给std::string用的std::getline一样,一不小心掉进去的话连错哪里都不知道,因为程序员面对这一类的库,一般第一个只会想到是自己代码哪里不对了吧?

不过后来我又测试了.net 3.0 3.5 4.0 4.5,结论是2.0 3.0 3.5会出现这样的问题,而4.0 4.5不会。和是不是Client Profile没有关系。升级4.0是最直接的办法了。

这些当作标准库用的东西的Bug,因为不知道什么时候会碰到,也不知道到底有多少,总觉得……挺那啥的。

哦对了之前看Boost.Asio库的时候,也遇到有这样的:

Remarks

Calls to cancel() will always fail with boost::asio::error::operation_not_supported when run on Windows XP, Windows Server 2003, and earlier versions of Windows, unless BOOST_ASIO_ENABLE_CANCELIO is defined. However, the CancelIo function has two issues that should be considered before enabling its use:

  • It will only cancel asynchronous operations that were initiated in the current thread.
  • It can appear to complete without error, but the request to cancel the unfinished operations may be silently ignored by the operating system. Whether it works or not seems to depend on the drivers that are installed.

For portable cancellation, consider using one of the following alternatives:

  • Disable asio's I/O completion port backend by defining BOOST_ASIO_DISABLE_IOCP.
  • Use the close() function to simultaneously cancel the outstanding operations and close the socket.

When running on Windows Vista, Windows Server 2008, and later, the CancelIoEx function is always used. This function does not have the problems described above.

来源

这种东西到底怎么躲得掉了啦……写代码的工作好可怕><

发表评论