1

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?

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
workaholic47
  • 151
  • 4
  • OOM can also mean out of resources. Based on those tiny snippets that may be the case. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Feb 07 '18 at 19:17
  • Possible duplicate of [System.Drawing Out of Memory Exception](https://stackoverflow.com/questions/6506089/system-drawing-out-of-memory-exception) – InbetweenWeekends Feb 07 '18 at 19:21
  • I'm sorry but I'm new here, just joined a few hours ago. Apologies if my question is vague. Any idea what might be happening though? – workaholic47 Feb 07 '18 at 19:22
  • Are any of the points on the failed graph outside those of a tan(x) graph that succeds? – Andrew Morton Feb 07 '18 at 19:22
  • @AndrewMorton I'm not sure but I doubt it. Regardless, there is a block I already inserted that makes sure no points to be plotted fall outside of the minimum and maximum bounds of my computer screen (+ or - 999,999,999 pixels in my case). It automatically flags those as asymptotes if it encounters them and doesn't even try to draw them...if that's what you were asking of course – workaholic47 Feb 07 '18 at 19:27
  • @3247 I suggest that you reduce those limits to, say, +/- 32767 pixels as GDI+ isn't good with large numbers. Just for a quick check. The OOM exception in GDI+ just means "something went wrong." – Andrew Morton Feb 07 '18 at 19:29
  • @AndrewMorton I set a breakpoint and it seems you're right. It's not actually out of memory. The exception is being thrown when the graph tries to draw one segment that had only 2 points. I never considered this as a problem and my original block of code only made sure that more than ONE point was plotted. I don't understand why the DrawCurve function would have a problem with 2 points. Anyways, I altered the code and forbade segments with less than FIVE points instead and the error vanished!!!! Why the hell would they say OOM when it's just an unknown exception? Thanks man – workaholic47 Feb 07 '18 at 19:37
  • @AndrewMorton I'm sorry but I just joined the site and still getting acquainted with the layout. How do I set an answer? – workaholic47 Feb 07 '18 at 19:51
  • 1
    `G.DrawCurve(New Pen(...), pts)` - Bad form there. You create a Disposable object (Pen) with no way to dispose it except for the GC. – TnTinMn Feb 07 '18 at 20:23
  • Interesting. You think it would be better to explicitly create a Pen object? To be honest I'm still studying VB and I have no idea how the GC or Dispose methods actually work. On that note, any ideas where I can learn more about this stuff? – workaholic47 Feb 13 '18 at 16:01

1 Answers1

1

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

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
workaholic47
  • 151
  • 4