Solved

What is Best Event Handler to Dispose of Unmanaged Objects?

Posted on 2014-10-15
6
116 Views
Last Modified: 2014-10-15
Where is Best Event Handler to Dispose of Unmanaged Objects? FormClosing or FormClosed? Using VB.net 2005.
0
Comment
Question by:gcook17
  • 3
  • 2
6 Comments
 
LVL 40
ID: 40382720
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.
0
 

Author Comment

by:gcook17
ID: 40382732
OK thanks. The objects are created at Form_Load and used until form is closed.
0
 

Author Comment

by:gcook17
ID: 40382735
Sorry I meant to say "managed" not "unmanaged"
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 15

Expert Comment

by:ChloesDad
ID: 40382910
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
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40382980
If these are controls, and if you added them to the Controls collection of the form as you should normally do when adding controls dynamically, you have nothing to do. The Dispose method mentioned by ChloesDad will do the job for you.

If they are not controls and are used only in Form_Load, then you dispose of them as soon has they are not needed.

If they are used in many places in the form and you know a time when they will not be needed anymore, then dispose of them at that point.

If they are used in many places in the form and you cannot determine a point where they will not be needed anymore, then Form_Closing is a good place.
0
 

Author Closing Comment

by:gcook17
ID: 40383034
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.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now