none
OutOfMemory Exception by calling Graphics.DrawLines RRS feed

  • Question

  • This seems a bug from .NET framework. We recreated this problem by the following codes and you can see just these few lines simple code will throw that OutOfMemory Exception. (My machine is Win 7 X64, 8G Memory, Quad Core) 

     

        Dim graphics As Graphics = Me.CreateGraphics()

            Dim pen As Pen = New Pen(Color.Black)

            pen.StartCap = Drawing2D.LineCap.ArrowAnchor

            pen.EndCap = Drawing2D.LineCap.DiamondAnchor

            ' The following data is generated from your code

            Dim points As PointF() = New PointF() {New PointF(187.7159, 241.4171), New PointF(187.3625, 242.0487), New PointF(186.9632, 242.7468), New PointF(186.8015, 243.0249)}

            graphics.DrawLines(pen, points)

     

    If you debug at visual studio, make sure the Exceptions setting is "CLR Exception Thrown".

     

    Thanks,

    James

    Friday, February 18, 2011 1:54 AM

Answers

  •  Apparently, if this exception is cause by the lines with length 0, supposing that we remove the last point in the array, the same exception should be thrown. However, we’ve tested this case before and the exception never occurred. Furthermore, if the EndCap is not set, this exception should also be thrown according to this answer, but in fact, we don’t see any exception at all.
    You are verifying the answer.  If you remove the zero length line, you don't get an exception. If you remove the end cap you don't get an exception.  Modify your code to eliminate zero length lines.
    • Marked as answer by Paul Zhou Friday, February 25, 2011 2:36 AM
    Wednesday, February 23, 2011 9:31 AM

All replies

  • GDI+ error descriptions aren't very descriptive.  Just correct your code and the error will disappear. Look at www.bobpowell.net for assistance with .NET graphics.
    Friday, February 18, 2011 3:45 AM
  • Hi,

    Graphics and Pen implements IDisposable and should be disposed.

    So maybe it is simply a memory leak which of course results in a out of memory - esp if you have the leak in a function that is called very often (e.g. whenever a window is drawn again).

    So use the using statement or ar least put a pen.Dispose and graphics.Dispose() at the end of the codeblock (Which is not that nice, because in case of an exception, it will not be disposed but maybe you can even live with that).

    With kind regards,

    Konrad

    Friday, February 18, 2011 2:13 PM
  • Hi James,

     

    Welcome to the MSDN forum!

     

    I think it is just some wrong with your code to draw a line. About how to draw a line via VB.NET, please refer to the MSDN document: Graphics.DrawLines Method that includes a code example.

     

    I hope this can help to resolve your problem.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Tuesday, February 22, 2011 2:30 AM
    • Unmarked as answer by JamesGraylock Wednesday, February 23, 2011 9:22 AM
    Monday, February 21, 2011 3:21 AM
  • Hmm. I tested a little and I was a little amazed. The function takes an array of floats, but it seems that the function cannot handle the float numbers.

    I just changed the part .xxxx to .0 and the error is gone.

    My test-function is now:

      Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim graphics As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        pen.StartCap = Drawing2D.LineCap.ArrowAnchor
        pen.EndCap = Drawing2D.LineCap.DiamondAnchor
        ' The following data is generated from your code
        Dim points As PointF() = New PointF() {New PointF(187.0, 241.0), New PointF(187.0, 242.0), New PointF(186.0, 242.0), New PointF(186.0, 243.0)}
        graphics.DrawLines(pen, points)
        pen.Dispose()
      End Sub
    

    It is quite unexpected result in my eyes but maybe it helps you to get your program up and running.

    With kind regards,

    Konrad

    Monday, February 21, 2011 5:28 AM
  • Hi Konrad,

    it is getting more strange. Because if you change the 'StartCap' value it works also with floats as points.

    Private Sub Main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
     Dim pen As Pen = New Pen(Color.Black)
    
     pen.StartCap = Drawing2D.LineCap.Flat
     pen.EndCap = Drawing2D.LineCap.DiamondAnchor
    
     ' The following data is generated from your code
     Dim points As PointF() = New PointF() {New PointF(187.7159, 241.4171), New PointF(187.36250000000001, 242.0487), New PointF(186.9632, 242.74680000000001), New PointF(186.80099999999999, 243.0249)}
     e.Graphics.DrawLines(pen, points)
     pen.Dispose()
    
    End Sub
    

    It looks like there a several combinations they do not work

    Thomas

     

    Monday, February 21, 2011 12:15 PM
  • Zero length lines with caps cause problems.
    • Marked as answer by Paul Zhou Tuesday, February 22, 2011 2:30 AM
    • Unmarked as answer by JamesGraylock Wednesday, February 23, 2011 9:21 AM
    Monday, February 21, 2011 12:47 PM
  • Ok, so this seems to be an issue for the connect website so microsoft can evaluate / fix it.

    At the moment I am still at work so I cannot spend time to check the connect webpage for an existing entry or create a new one. But in about 6 hours I could do that (if noone else feels like doing it in between :) ).

    With kind regards,

    Konrad

    Monday, February 21, 2011 12:47 PM
  • Zero length lines with caps cause problems.


    Ahh ... that could even make sense. When the length is zero and you want to print an arrow -> In which direction should it go? So this sounds like a good explanation John!

    With kind regards,

    Konrad

    Monday, February 21, 2011 12:50 PM
  • Zero length lines with caps cause problems.


    Ahh ... that could even make sense. When the length is zero and you want to print an arrow -> In which direction should it go? So this sounds like a good explanation John!

    With kind regards,

    Konrad


     Apparently, if this exception is cause by the lines with length 0, supposing that we remove the last point in the array, the same exception should be thrown. However, we’ve tested this case before and the exception never occurred. Furthermore, if the EndCap is not set, this exception should also be thrown according to this answer, but in fact, we don’t see any exception at all.

    James
    Wednesday, February 23, 2011 9:22 AM
  •  Apparently, if this exception is cause by the lines with length 0, supposing that we remove the last point in the array, the same exception should be thrown. However, we’ve tested this case before and the exception never occurred. Furthermore, if the EndCap is not set, this exception should also be thrown according to this answer, but in fact, we don’t see any exception at all.
    You are verifying the answer.  If you remove the zero length line, you don't get an exception. If you remove the end cap you don't get an exception.  Modify your code to eliminate zero length lines.
    • Marked as answer by Paul Zhou Friday, February 25, 2011 2:36 AM
    Wednesday, February 23, 2011 9:31 AM