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?