Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


Disposing objects and releasing resources in VB .Net

  • Please log in to reply
No replies to this topic

#1 aommaster


    I !<3 malware

  • Malware Response Team
  • 5,294 posts
  • Gender:Male
  • Location:Dubai
  • Local time:09:15 PM

Posted 05 August 2009 - 08:27 AM


I'm currently working on a program that I decided to split up into classes (it made managing the program a lot easier).

Currently, each class has a constructor that opens a connection to a database, creates a data adapter and a data set. Then, it writes the data from the database table to the dataset, and finally, generates a dataview (due to the fact that I can sort and hence, find indexed data quickly. On that note, this is good programming practice, yes?). This Dataview is stored in the object as a property, to which other functions can then access.

The main body of the code will call this object (hence, opening the link to the DB, and in essence, generate the dataview), call a number of functions (these search for entries in the database, and the function will return a requested value), perform a few other functions, and then, will move on to another object.

My problem is this: I haven't gotten rid of the object. This means that somewhere in memory, is an area allocated to that dataview, yes? How do I tell VB .Net that this object is no longer needed and whatever memory resources were used by that can be cleared?

I believe I have to use the dispose function, so I wrote something like this into my class:
Sub Dispose() Implements System.IDisposable.Dispose
		Static done_before As Boolean = False
		If done_before Then Exit Sub
		done_before = True
End Sub
(I also had an Implements IDisposable at the top of the class).

However, I don't believe that I am doing this right, because after I called the dispose function, I was still able to call a function inside the object.

What would be the correct way of doing this in terms of good programming practice?

Thanks everyone!

Edited by aommaster, 05 August 2009 - 08:28 AM.

My website: http://aommaster.com
Please do not send me PM's requesting for help. The forums are there for a reason : )
If I am helping you and do not respond to your thread for 48 hours, please send me a PM

BC AdBot (Login to Remove)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users