Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
|
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2009, 05:36 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default Importants of disposing of objects

How important is it ot dispose of objects when finished with them, as it does free up resources but will it actually increase performance?
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old March 4th, 2009, 01:00 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

It is good practice to never rely on the Garbage Collector (GC) to clean up your objects for you. Not only will this improve the preformance of your application over it can help prevent strange errors when dealing with, for example, I/O operations.

A good example of why you should do this would be sending an email with an attachment from the local file system. When you call this line of code:

mailMsgObj.Attachments.Add(new Attachment(somefile))

A lock is actually going to be placed on the file that is specificed in 'somefile' and this lock will remain in place until mailMsgObj.Dispose() is called to release the resource.


To avoid calling Dispose() on all of my objects I prefer using statements such as:

vb Code:
Using <someObject>
End Using

csharp Code:
using(<someobject>)
{
}

Once execution has passed outside the scope of the using block your objects will be destroyed.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
 
Old March 11th, 2009, 11:18 AM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 244
Thanks: 3
Thanked 4 Times in 4 Posts
Default

So if i wasn't using the using statement then i should do
Code:
someobject.Dispose()
__________________
Apocolypse2005, I'm a programmer - of sorts.
 
Old March 11th, 2009, 11:25 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Yes. Provided the object exposes a Dispose method of course.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
Value objects rayback_2 BOOK: Beginning Cryptography with Java 1 May 13th, 2008 05:04 PM
Disposing / Releasing objects yamyam C# 5 February 17th, 2006 04:06 AM
Correctly disposing dynamically created controls simarik C# 2 December 20th, 2005 08:49 PM
Disposing a WinForm in Excel Bowmangotti VB.NET 2002/2003 Basics 0 October 5th, 2004 10:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.