捕获 OutOfMemoryException 使调试变得困难

2024-01-05

当我调试程序并尝试在立即窗口中执行某些操作时,有时会在立即窗口中显示一条错误消息:

由于内存不足,函数评估被禁用 例外。

它还显示,当通过将鼠标悬停在对象上来查看对象的属性时。

在尝试找到问题的原因后,我将其范围缩小到这个小代码示例:

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. 最后,我怎样才能防止这种情况发生?

是的,这是预期的行为。

您需要让调试器运行(单步执行或在下一行放置断点并单击 F5)才能从这种状态恢复。即使有时它也没有帮助,并且运行直到您命中堆栈上更高的其他函数通常会使调试器再次合作。

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

更多信息 - MSDN功能评估已禁用... http://msdn.microsoft.com/en-us/library/ms234762.aspx, SO - 由于先前的函数评估超时,函数评估被禁用 https://stackoverflow.com/questions/2721108/function-evaluation-disabled-because-a-previous-function-evaluation-timed-out

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

捕获 OutOfMemoryException 使调试变得困难 的相关文章

随机推荐