I'll start with what I know.
One should dispose objects that:
- implement IDisposable interface
- have Dispose() method (meaning they don't implement IDisposable directly, but are derived classes from class, who has implemented IDisposable interface)
Additionally one should implement IDisposable interface to ones one classes, when they are accessing external (unmanaged) resources. And of course dispose them after they've been used.
How to know which objects implement IDisposable interface?
Search for MSDN ClassName in google.com or similar, and take a look under the Syntax section (second big blue text from the top usually). For example SpeechSynthesizer
'Declaration Public NotInheritable Class SpeechSynthesizer _ Implements IDisposable
How to know which objects implement Dispose() method?
Same as above: take a look at MSDN or use F2 (object browser) in Visual Studio, and search there for what your interest is.
How to dispose objects?
If you can, do it with Using block:
'needs reference to System.Speech (right click on project, and add reference) Using mySpeechObject As New System.Speech.Synthesis.SpeechSynthesizer mySpeechObject.Speak("Something to speek!") End Using 'another example Using myReader As New IO.StreamReader("C:\test.txt") Dim textFromFile = myReader.ReadToEnd() End Using
Why "if you can"? In example code above, how would you use SpeakAsync method for mySpeechObject?
So how to do it, if you can't do it in Using block? By calling Dispose() method on the object. Let's continue with SpeechSynthesis class example:
Sub Main() Dim mySpeechObject As New System.Speech.Synthesis.SpeechSynthesizer mySpeechObject.SpeakAsync("Something to speek!") AddHandler mySpeechObject.SpeakCompleted, AddressOf SpeachEnded End Sub Private Sub SpeachEnded(sender As Object, e As Speech.Synthesis.SpeakCompletedEventArgs) TryCast(sender, SpeechSynthesizer).Dispose() 'if you're having Option Strict On 'sender.Dispose() 'if you're having Option Strict Off End Sub
When you don't need to dispose object, even if it implements IDisposable, or inherits Dispose()?
*Warning*: example below is just example, and doesn't represent good programming practice. Example class EncapsulatedObject should implement IDisposable, and be disposed when out of use!
When object, that should be disposed, is encapsulated by managed code, that consequentially disposes it with itself:
'form example: Option Strict On Imports System.Speech.Synthesis Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim myInstanceOfEncapsulatedObject As New EncapsulatedObject Using myReader As New IO.StreamReader("C:\test.txt") 'put in test.txt really long text myInstanceOfEncapsulatedObject.SpeekText(myReader.ReadToEnd()) End Using End Sub End Class Class EncapsulatedObject Private mySpeechObject As New System.Speech.Synthesis.SpeechSynthesizer Public Sub SpeekText(text As String) mySpeechObject.SpeakAsync(text) End Sub End Class
Why EncapsulatedObject in previous example should implement IDisposable? Try the following example (edit:and close the app, while it's still speaking):
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim myInstanceOfEncapsulatedObject As New EncapsulatedObject myInstanceOfEncapsulatedObject.SpeekText("Really long text example: All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.") End Sub End Class Class EncapsulatedObject Private mySpeechObject As New System.Speech.Synthesis.SpeechSynthesizer Public Sub SpeekText(text As String) mySpeechObject.SpeakAsync(text) End Sub End Class
A rule of thumb for disposing objects
Each time you know you are dealing with outer resources (files, databases,...) dispose such objects. Better be safe than sorry />/>
Note: I'm aware there could be wrong information in what I just posted, and that's why I opened this topic. I'd like to have a debate on disposing objects, so everyone who is not sure about it, could benefit.
This post has been edited by lucky3: 27 November 2012 - 10:55 AM