473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Correct way of using Dispose

I always dispose any object with the following:
If not (obj1 is nothing) then obj1.dispose

Now I wonder if that is the correct way of disposing an object? Should I use
obj1 = Nothing after the dispose? Pls advice. Thanks.
Jul 21 '05 #1
9 1492
kjon <y@y.co> wrote:
I always dispose any object with the following:
If not (obj1 is nothing) then obj1.dispose

Now I wonder if that is the correct way of disposing an object? Should I use
obj1 = Nothing after the dispose? Pls advice. Thanks.


Assuming it's a local variable which isn't used again, or an instance
variable in an instance which is about to become eligible for garbage
collection, there's no need to do this.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
You do not need to set anything to Nothing. You only needed to do that in
VB6.

"kjon" <y@y.co> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
I always dispose any object with the following:
If not (obj1 is nothing) then obj1.dispose

Now I wonder if that is the correct way of disposing an object? Should I use obj1 = Nothing after the dispose? Pls advice. Thanks.

Jul 21 '05 #3
In addition to Marina,

I think see means objects you can set properties to Nothing by instance a
datasource or a binding.
(Or did I misunderstood it Marina?)
Cor
You do not need to set anything to Nothing. You only needed to do that in
VB6.

Jul 21 '05 #4
This is standard misconception and recipe for future problems. GC is able to
cope only with limited number of allocations per time unit and is not ideal,
read has limitations. If it is not able to detect that reference could be
freed, related resource might survive through many GC cycles.
See
http://msdn.microsoft.com/architectu...l/scalenet.asp
for additional details.

HTH
Alex
You do not need to set anything to Nothing. You only needed to do that in VB6.


Jul 21 '05 #5
> Marinawrote:
You do not need to set anything to Nothing. You only needed to do
that in VB6.

Actually, in wasn't even required in VB6 - it was required I think
only as late as VB4 due to a bug in VB4, but was endlessly
perpetuated by well-meaning developers after that.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 21 '05 #6
Hi Alex,

What do you want to say with that?

And please send it next time as an answer for Marina, now see maybe does not
see it because you answered a message from me.

I know that document.

Cor
This is standard misconception and recipe for future problems. GC is able to cope only with limited number of allocations per time unit and is not ideal, read has limitations. If it is not able to detect that reference could be
freed, related resource might survive through many GC cycles.
See
http://msdn.microsoft.com/architectu...l/scalenet.asp for additional details.

HTH
Alex
You do not need to set anything to Nothing. You only needed to do that in VB6.



Jul 21 '05 #7
AlexS <sa***********@SPAMsympaticoPLEASE.ca> wrote:
This is standard misconception and recipe for future problems. GC is able to
cope only with limited number of allocations per time unit and is not ideal,
read has limitations. If it is not able to detect that reference could be
freed, related resource might survive through many GC cycles.
In the vast majority of cases, however, it does *not* help to set
variables to null/Nothing.
See
http://msdn.microsoft.com/architectu...l/scalenet.asp
for additional details.


Note that that specifically says not to bother with it for local
variables.

In my experience, it's very rare to have members which are unneeded
until the rest of the object becomes eligible for garbage collection.
In fact, I can't remember the last time it made sense to set something
to null just for the sake of garbage collection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
> In my experience, it's very rare to have members which are unneeded
until the rest of the object becomes eligible for garbage collection.
In fact, I can't remember the last time it made sense to set something
to null just for the sake of garbage collection.


Like I said it depends on application. Local variables allocated on stack -
I agree, there is not much sense in nulling them. However, I would suggest
to check the topic "Set Unneeded Member Variables to Null Before Making
Long-Running Calls" in the link I recommended. It explains some of caveats
with local variables. Problem is that any heap allocation under certain
conditions can survive GC cycles for very long periods of time. Which
finally can cause troubles. That's why I suggest not to adopt generic rules,
which might work or not. Definition of long call is very application
specific.
HTH

Alex
Jul 21 '05 #9
AlexS <sa***********@SPAMsympaticoPLEASE.ca> wrote:
In my experience, it's very rare to have members which are unneeded
until the rest of the object becomes eligible for garbage collection.
In fact, I can't remember the last time it made sense to set something
to null just for the sake of garbage collection.


Like I said it depends on application. Local variables allocated on stack -
I agree, there is not much sense in nulling them. However, I would suggest
to check the topic "Set Unneeded Member Variables to Null Before Making
Long-Running Calls" in the link I recommended. It explains some of caveats
with local variables. Problem is that any heap allocation under certain
conditions can survive GC cycles for very long periods of time. Which
finally can cause troubles. That's why I suggest not to adopt generic rules,
which might work or not. Definition of long call is very application
specific.


Yes, but I would be wary of variables which can be set to null like
this - usually it indicates something being a member variable which
should have been a local variable in the first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marius Rus | last post by:
I have a project in C# that do the followings: From an csv text file it is taking datas and inserted into an paradox database. My problem it is that when writing into the paradox database it is...
6
by: Rob Meade | last post by:
Hi all, Ok - I've used the word gramatically - which I'm sure is incorect, however I mean when VS2005 lists "warnings" like this one: Warning 1 Variable 'Command' is used before it has been...
2
by: Julio Allegue | last post by:
I am getting the wrong Count(*) on vb.net using the ExecuteScalar . It returns all the rows. It doesn't seem to look at the WHERE clause. At the same time, I am getting the correct count on "SQL...
0
by: Atara | last post by:
regarding the answers I got from the thread "Does each "New" needs a corresponding "Dispose" " (http://www.developersdex.com/vb/message.asp?p=&r=3582570&page=2) I have some questions: Till now,...
3
by: Chris | last post by:
I am just wondering if I did this the best way possible. I needed to add a double boarder around a label box. So I made a pen in the class, and do the drawing of the rectangles in the onpaint...
9
by: kjon | last post by:
I always dispose any object with the following: If not (obj1 is nothing) then obj1.dispose Now I wonder if that is the correct way of disposing an object? Should I use obj1 = Nothing after the...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
7
by: garyusenet | last post by:
Hi I have finally made progresss with my use of the open file dialog box! I have now made my own method that opens the dialog box and returns the file selected. Can you please have a quick...
9
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.