2014-09-19 56 views
3

当我调试我的程序,并尝试做某些事情在即时窗口,它有时会显示在即时窗口中的错误消息说:陷入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抛出时,该文件不是一个形象,错误...

问题:

  1. 难道上面的代码示例产生问题给其他人?
  2. 有人可以澄清这一点吗?为什么会发生这种情况?
  3. 最后,我该如何防止这种情况?
+0

应该抛出一个'FileNotFoundException'如果没有找到该文件。如果文件没有有效的图像格式,它会抛出OOME。或GDI +不支持该文件的像素格式。http://msdn.microsoft.com/en-us/library/stf701f5(v = vs.110).aspx – 2014-09-19 18:28:55

+0

顺便说一句,我很高兴见到把这个错误与OOME关联的人。 – 2014-09-19 18:31:10

+2

调试器将停止尝试评估手表是否发生了非常恶劣的事情。为了确保这种肮脏不会一遍又一遍地发生。它不会重置该状态,直到您继续调试器。是的,非常可悲的是GDI +例外并不总是意味着你实际上是OOM。尽管如此,调试会话完成后,您必须做一些激烈的事情才能得到修复。 – 2014-09-19 18:31:52

回答

0

是的,这是预期的行为。

您需要让调试器运行(在下一行跳过或放置断点并单击F5)以使其从此状态恢复。即使有时它并没有帮助和运行,直到你在栈上打高一些的函数通常会使调试器再次合作。

请注意,OOM不是唯一的情况 - 即立即窗口中的长时间运行代码将使调试器进入相同的状态。

更多信息 - MSDN Function evaluation is disabled...,SO - Function evaluation disabled because a previous function evaluation timed out