Solved

What is Best Event Handler to Dispose of Unmanaged Objects?

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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
OK thanks. The objects are created at Form_Load and used until form is closed.
0
 

Author Comment

by:gcook17
Comment Utility
Sorry I meant to say "managed" not "unmanaged"
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:ChloesDad
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now