Solved

What is Best Event Handler to Dispose of Unmanaged Objects?

Posted on 2014-10-15
6
120 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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