Out of memory exception.

asked 2020-08-29T06:06:52.343+00:00
Kodeeswaran Duraisamy 161 Reputation points

My problem is that the GDI handles (Brush, Bitmap, Font and DC) got increased everytime I load a form.
Eeven after I unload the form and dispose the form, they remain the same (not decreased). Is there any method to dispose the GDI handles?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
23,703 questions
{count} votes

Accepted answer
  1. answered 2020-09-01T07:57:46.83+00:00
    Tianyu Sun-MSFT 12,846 Reputation points Microsoft Employee

    Hello Kodeeswaran,

    When you use GDI Objects(Brush, Bitmap, Font and so on) and declare them in the scope of the Form class, you need to correctly dispose of the object before the form is closed (disposed).

    For example:

        Public Class Form1   
            Private pen As Pen = New Pen(Color.Red)  
            Private Sub panelPaint_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)  
                e.Graphics.DrawRectangle(pen, New Rectangle(10, 10, 100, 100))  
            End Sub  
      
            Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)  
                pen.Dispose()  
            End Sub  
        End Class  
    

    Besides, our forum is about the usage of tools and editors in Visual Studio, and Visual Basic forum is currently not supported in the Microsoft Q&A forums, I recommend you can redirect and post a new thread to our MSDN Visual Basic forum.

    Best Regards,
    Tianyu


0 additional answers

Sort by: Most helpful