Caught OutOfMemoryException makes it difficult to debug

When I debug my program and try to do some things in the immediate window, sometimes I get an error message in the nearest window:

Feature evaluation was disabled due to out of memory exception.

It also shows that when viewing the properties of an object by hovering them.

After trying to find the cause of the problem, I narrowed it down to this little code:

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
    class Program
        static void Main(string[] args)
                //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


The debugger seems to fade away when an OutOfMemoryException is thrown, even when it caught ...

I can imagine that no one ever thought it would be possible to debug a program that had an OutOfMemoryException. But unfortunately Image.FromFile throws this error when the file is not an image ...


  • Is the above code example causing anyone else a problem?
  • Can someone please clarify this a bit? Why is this happening exactly?
  • Finally, how can I prevent this?

source to share

1 answer

Yes, this is expected behavior.

You have to let the debugger run (step over or put a breakpoint on the next line and press F5) to recover from that state. Even sometimes it doesn't help and works until you hit some other function higher in the stack, usually making the debugger cooperate again.

Note that OOM is not the only case - that is, long code in the Immediate window will get the debugger into the same state.

More Information - MSDN Feature Rejection Disabled ... SO - Feature Rejection Disabled Because a Previous Feature Evaluation Was Delayed



All Articles