gcook17
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.
ASKER
OK thanks. The objects are created at Form_Load and used until form is closed.
ASKER
Sorry I meant to say "managed" not "unmanaged"
If they need disposing then there is an overridable method called Dispose on every form
add your code in there as required
'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
add your code in there as required
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.