Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

What is Best Event Handler to Dispose of Unmanaged Objects?

Posted on 2014-10-15
6
119 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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