Solved

What is Best Event Handler to Dispose of Unmanaged Objects?

Posted on 2014-10-15
6
117 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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