Link to home
Start Free TrialLog in
Avatar of gcook17
gcook17Flag for United States of America

asked on

What is Best Event Handler to Dispose of Unmanaged Objects?

Where is Best Event Handler to Dispose of Unmanaged Objects? FormClosing or FormClosed? Using VB.net 2005.
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

It depends on where they are declared, at what scope, and the way you use them.

The best place to dispose of objects is usually as soon as you do not need them anymore.

And for Unmanaged objects, you do not really dispose of them, you release them, often with the ReleaseComObject method.
Avatar of gcook17

ASKER

OK thanks. The objects are created at Form_Load and used until form is closed.
Avatar of gcook17

ASKER

Sorry I meant to say "managed" not "unmanaged"
If they need disposing then there is an overridable method called Dispose on every form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

Open in new window


add your code in there as required
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gcook17

ASKER

These are database objects and are used potentially till form is closed. Therefore I will place in Form_Closing Event handler. Previously most were in Form_Closed. Thank you.