當我調試我的程序,並嘗試做某些事情在即時窗口,它有時會顯示在即時窗口中的錯誤消息說:陷入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