1

I appreciate that this is a rather broad question, so I'm going to specify details to try to explain why I am struggling to understand the process.

As far as I am aware, VB.Net and the .NET framework generally, has inbuilt Garbage Collection, which means when I Dim a variable and assign a value to it, I don't need to declare it null, or "dispose" of it after.

Recently, in another question, I was reminded about disposing my Pen and Brush objects in this code example:

    Private Sub pbProfilePicture_Paint(sender As Object, e As PaintEventArgs) Handles pbProfilePicture.Paint
        Dim myPen As Pen
        Dim myBrush As Brush

        myPen = New Pen(Drawing.Color.FromArgb(180, 204, 112), 1)
        myBrush = New SolidBrush(Color.FromArgb(180, 204, 112))
        Dim myGraphics As Graphics = e.Graphics
        myGraphics.DrawEllipse(myPen, 28, 28, 12, 12)
        myGraphics.FillEllipse(myBrush, 28, 28, 12, 12)
    End Sub

I was told to use either the Using command or dispose of them manually (assuming .Dispose() for each object). My question is, why do I need to dispose of the objects in this case? Is there a rule of thumb for what objects need to be disposed and what is handled automatically? As far as I can tell from documentation, it seems to follow the pattern of disposing anything that is initialised with the New keyword.

Are the consequences of not disposing objects, clogging up and increasing memory usage in the application?

Any help on where I can get started on this would be really useful.

RazorKillBen
  • 561
  • 2
  • 20
  • When an object you created exposes the public `Dispose()` method, you dispose of it either implicitly (`Using` statement: e.g., `Using [object] as [Type] = New [Type]() ... End Using`) or explicitly (calling `[object].Dispose()`). – Jimi Feb 01 '20 at 16:31
  • It's more related to the unmanaged resources allocated, GDI+/User handles etc. – Jimi Feb 01 '20 at 16:36
  • Thanks @Jimi - so everytime an object is created, check if the `.Dispose()` method is able to be used as a way to check each object? That's exactly what I wanted, a way to check/test and learn. – RazorKillBen Feb 01 '20 at 16:39
  • 1
    You can use it as a rule of thumbs. When an object exposes that method, it means (most of the time) that you need to call it, because there might be unmanaged resources that need to be freed. Think about the FileStream class: when you create one, you have to call its `Close()` or `Dispose()` (but you call `Dispose()`, as the default M.O.) methods to release the file handle. Otherwise, the File remains locked. The same applies to other objects that may wrap unmanaged resources (Pen, Brush, Font, Bitmap etc. - all GDI+ objects - are disposable objects). – Jimi Feb 01 '20 at 16:43
  • That makes perfect sense - thanks so much for taking the time to explain. – RazorKillBen Feb 01 '20 at 17:09
  • 1
    Does this answer your question? [Do you need to dispose of objects and set them to null?](https://stackoverflow.com/questions/2926869/do-you-need-to-dispose-of-objects-and-set-them-to-null) – Lex Li Feb 01 '20 at 19:36
  • @Lex Li Too old :) This one is better, IMO: [Implementing a Dispose method](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose). The more interesting part is the discussion in the feedback section, where also Stephen Taub gives his guidelines on this matter. – Jimi Feb 01 '20 at 20:33
  • The garbage collector addresses memory usage from objects created using `New` (and ONLY memory usage from objects created using `New`). `Dispose` and `IDisposable` are for everything else that needs to be released before the program terminates. – Craig Feb 03 '20 at 14:38

0 Answers0