Search code examples
vb.netbitmapout-of-memorygraphing

OutOfMemory exception thrown while drawing on bitmap


I've been working on a graphing software for a few months now and it's nearly complete. Only problem is the odd OutOfMemory exception I keep getting when drawing on the Bitmap

First, I declare a global bitmap variable (with no instantiation) on which I will draw the cartesian plane and the graph. When it's done I will then set it as the image of a PictureBox on the form.

Private bmp As Bitmap 

Once the user enters the function to be drawn, I call a procedure inside which I firstly instantiate the Bitmap variable before creating a Graphics object which I will use to call the Draw procedures:

bmp = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim G As Graphics = Graphics.FromImage(bmp)

Once this is done, I call a loop structure that will then calculate the points of the function entered, storing them in a Point object array I will use in the DrawCurve function.

Dim pts() As Point
.
.
<loop here>
.
.
G.DrawCurve(New Pen(...), pts)

If it detects an asymptote, it exits the loop and goes on to draw the part of the graph that has been calculated so far, then returns to the beginning of the loop and continues onwards after the asymptote. I've tested this on several graphs that had numerous asymptotes (eg tan[x]) and it worked well. However, there is one graph I tried to draw that threw an exception. It was "sin(x^log(x))^tan(x)" if that helps. The graph itself has a few asymptotes and after it drew about 3 or 4 segments, it threw an OutOfMemory exception when it tried to execute the DrawCurve procedure

G.DrawCurve(New Pen(...), pts) 'OutOfMemory exception thrown

I've already checked and the application is using less than 25 MB of RAM, about 10 or so GDI objects and not a lot of handles. I don't understand where the OutOfMemory exception is coming from. Everything is in order and I should point out that more complex graphs, with even more asymptotes are working well. I have tried everything but the same exception is thrown every single time. It doesn't matter if the computer is still fresh from a reboot or has been running for hours. Doesn't matter if I use some software to free up RAM. And in fact, it's actually saying there is still quite a lot of memory left but the program keeps throwing the OutOfMemory exception when I try drawing on the bitmap (so far the one graph which I mentioned is doing this and I haven't yet found any others). I don't know what could be happening. Anyone have any clue what might cause the DrawCurve function on a Bitmap to throw an OutOfMemory exception?


Solution

  • Turns out the OutOfMemory exception was a misnomer, as the actual culprit was an invalid argument being passed to the DrawCurve function. Apparently a Point array containing just 2 objects cannot be processed and an exception was being thrown, confusingly termed "OutOfMemory".

    I'm yet to perform more tests and try to recreate the error, but for now, I just inserted a block of code that checked to see that at least 5 points are contained in the Points array before passing it on to the DrawCurve function. Sorted out the mess and the graph came out nice and smooth.

    Oh, and btw, I am on Visual Studio 2010 so that might be an error that was long since fixed. Regardless, if anyone keeps getting an OutOfMemory exception that can't be traced down to an actual over-expenditure of resources, then likely it's a GDI object that's running into an exception it can't recognise and just blurting out that it's Out Of Memory. Check all arguments you might be passing and you might just get lucky