当我调试我的程序,并尝试做某些事情在即时窗口,它有时会显示在即时窗口中的错误消息说:陷入OutOfMemoryException异常使得调试困难
功能评价是禁用,因为出现内存不足 异常。
它还表明,通过将鼠标悬停在对象上来查看对象的属性时。
试图找到问题的原因后,我把范围缩小到这个小代码示例:
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
//outofmemoryexception can be thrown by Image.FromFile("path/that/does/not/exist.png")
//if the path points to a file that is not an image
throw new OutOfMemoryException();
}
catch (OutOfMemoryException ex)
{
//caught the exception
//so no problem, right?
}
//Random object to use in immediate window
Random rand = new Random();
//Also, try hovering over this regex and take a look at its properties.
var test = new Regex("");
//put a breakpoint here (at the next closing curly brace) and try calling rand.Next() in the immediate window
}
}
}
看来,调试怪胎当一个OutOfMemoryException发生,甚至当它抓住了...
我可以想象,从来没有人认为有可能调试出现OutOfMemoryException的程序。但可悲的是足够Image.FromFile抛出时,该文件不是一个形象,错误...
问题:
- 难道上面的代码示例产生问题给其他人?
- 有人可以澄清这一点吗?为什么会发生这种情况?
- 最后,我该如何防止这种情况?
应该抛出一个'FileNotFoundException'如果没有找到该文件。如果文件没有有效的图像格式,它会抛出OOME。或GDI +不支持该文件的像素格式。http://msdn.microsoft.com/en-us/library/stf701f5(v = vs.110).aspx – 2014-09-19 18:28:55
顺便说一句,我很高兴见到把这个错误与OOME关联的人。 – 2014-09-19 18:31:10
调试器将停止尝试评估手表是否发生了非常恶劣的事情。为了确保这种肮脏不会一遍又一遍地发生。它不会重置该状态,直到您继续调试器。是的,非常可悲的是GDI +例外并不总是意味着你实际上是OOM。尽管如此,调试会话完成后,您必须做一些激烈的事情才能得到修复。 – 2014-09-19 18:31:52