tags:

views:

256

answers:

8

This is something i seeem to get a different answer for, every time i ask a developer. So i thought it would be a good question for the overflow to see what everyone does.

If you have a sqlClient.sqlconnection object, in the cleanup code (finally block etc) do you dispose of the object or do you set it to nothing, or both?

I've always just called the dispose, and allowed the garbage collector to do its thing...

+1  A: 

You don't need to set the variable to Nothing, just disposing is enough.

Otávio Décio
It is _usually_ not necessary - but in rare situations it is helpful. Depending on the referring variable. Tha't why this question keeps coming back.
Henk Holterman
+1  A: 

Jeff Atwood had a recent blog post on this...

It seems it's not really necessary unless you don't trust the garbage collector. I've stopped doing this altogether and most often use the "using" syntax which is clean and easy to understand.

Andrew Flanagan
+3  A: 

You're basically doing the right thing. After you call .Dispose(), you can be sure that the resources allocated by the connection are cleaned up. Then, as soon as the SqlConnection object you declared in code goes out of scope, the GC will clean up that memory as well.

If you'd like to prod the GC, you can always call its .Collect() method.

Mike
I've had to use the gc.collect in one app over the past few years... was using an early version of ipWorks and had memory issues if i didn't force the garbage collector to free resources.
GordonB
I've seen a few cases as well - usually in wrappers to unmanaged libraries. The problem is that in .NET you never _expect_ a memory leak, so it takes forever to track down :)
Mike
Since this is currently the accepted answer, you should also look into the "Using" keyword in VB.Net.
Joel Coehoorn
Note that the GC does not clean up the memory "as soon as" the code goes out of scope, it does it whenever it feels like sometime after it goes out of scope.
Mystere Man
True enough - thanks Mystere Man. I was a little bit loose in my explanation there.
Mike
A: 

If you open up the code with the "= Nothing" in reflector you will see that the compiler has (usually) removed/optimized the statement.

Now if you do a check like this.

If myObject IsNot Nothing Then
  myObject = Nothing
End If

The compiler may not optimize this away, and this will cause you to hold a reference to myObject longer then necessary, causing the object to wait longer than necessary to be eligible for GC.

Ideally, you should dispose the object asap and leave it alone.

StingyJack
A: 

If the type has a Dispose use that.

If the type does not have a Dispose, and it holds on to large amounts of memory, then setting the reference to nothing before performing some slow operation (rather than holding on to it) can help avoid "mid life crisis".

However the compilers have become better at optimising this case and releasing the reference well before the end of the block.

Richard
+4  A: 

Have a look at my answer to a similar question. Setting a variable to null/Nothing is very rarely necessary. Calling Dispose is necessary.

Jon Skeet
A: 

I always ensure to dispose them off by employing an 'using' block but never set them to null. MSDN also does not ask us to do this.

I will be happy to learn any reasons others have to explicitly set the ref to null. Using the object after disposing would anyway throw an 'ObjectDisposedException' so accidental usage after dispose cannot be a reason.

Mohit Chakraborty
It **should** throw ObjectDisposedException, but that does rely on the vendor implementing Dispose correctly...
Rowland Shaw
Yup, agree. Good point.
Mohit Chakraborty
It is useful if the ref has a longer life (eg a Shared var)
Henk Holterman
A: 

I agree, disposing is mandatory as long as the object implements a dispose() method you have to call it, because when some object implements a dispose() method it means that he can't count on the GC to free the resources that he has been using (i.e. he is using unmanaged resources) in this case, yes you have to call dispose in order to release these unmanaged resources. about setting the reference to null, it's not necessary, but it's still a good programming practice no doubt. The really interesting thing here is, can anyone use a disposed object or an object with no references (i.e can a dead (or rather about to die) objects resurrect? Take a look at the following:

public class Employee {

    public static Employee emp;

    public string Name { get; set; }
    public string Position { get; set; }

    public override string ToString() {
        return String.Format("{0} works as {1}", Name, Position);
    }

    ~Employee() {
        emp = this;
    }
}
Galilyou