When I debug my program, and try to do certain things in the immediate window, it sometimes shows an error message in the immediate window saying:
The function evaluation was disabled because of an out of memory exception.
It also shows that when viewing the properties of object by hovering over them.
After trying to find the cause of the problem, I narrowed it down to this small code sample:
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
}
}
}
It seems that the debugger freaks out when an OutOfMemoryException occurs, even when it's caught...
I can imagine that no one ever thought it would be possible to debug a program that has had an OutOfMemoryException. But sadly enough Image.FromFile throws that error when the file is not an image...
Questions:
- Does the above code sample give problems to anyone else?
- Can someone clarify this a bit? Why does this happen exactly?
- And lastly, how can I prevent this?