3

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:

  1. Does the above code sample give problems to anyone else?
  2. Can someone clarify this a bit? Why does this happen exactly?
  3. And lastly, how can I prevent this?
user886079
  • 864
  • 2
  • 11
  • 18
  • It should throw a `FileNotFoundException` if the file isn't found. It throws the OOME if `The file does not have a valid image format.` or `GDI+ does not support the pixel format of the file.` http://msdn.microsoft.com/en-us/library/stf701f5(v=vs.110).aspx – Dave Zych Sep 19 '14 at 18:28
  • BTW, i'd be glad to meet the fellow that associated this error to OOME. – Patrice Gahide Sep 19 '14 at 18:31
  • 2
    The debugger will stop trying to evaluate watches if something very nasty happened. To make sure that nastiness doesn't happen over and over again. It doesn't reset that state until you continue debugger. Yes, fairly sad that the GDI+ exception doesn't always mean that you are actually OOM. Still, that debugging session is done anyway, you'll have to do something drastic to get this repaired. – Hans Passant Sep 19 '14 at 18:31
  • More on this weird association [here](http://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i) – Patrice Gahide Sep 19 '14 at 18:33
  • @Dave Zych: You're right, I made a mistake, it's when the file is not an image. – user886079 Sep 19 '14 at 18:38
  • @PatriceGahide interesting info about that here: http://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i?lq=1 – Dave Zych Sep 19 '14 at 18:39
  • @Dave Yes, that's the link I gave in my previous comment ;) One more proof that Stackoverflow's CSS for unvisited links needs to change! I want everyone to be working on this dreadful problem! – Patrice Gahide Sep 19 '14 at 18:51

1 Answers1

0

Yes, it is expected behavior.

You need to let debugger to run (step over or put breakpoint on next line and click F5) for it to recover from such state. Even that sometimes it does not help and running till you hit some other function higher on the stack usually makes debugger to cooperate again.

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

More information - MSDN Function evaluation is disabled..., SO - Function evaluation disabled because a previous function evaluation timed out

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179