Code Cleaning

  • Thread starter Thread starter Anthony leong kar keong
  • Start date Start date
A

Anthony leong kar keong

Hi i would like to ensure some code cleaning methods.

#1 COM objects
Is the following the correct way ?
objMe = null;

#2 Dispose VS. null

Mydataset.dispose();
or
Mydatatset =null

#3 c# class objects
myobject Myinstance = new myobject();
....
...
Myinstance =null <--correct ?


very thank you and sharing is very much appreciated
 
Hi,
#1 COM objects
Is the following the correct way ?
objMe = null;

To release a COM object, I believe you should use
System.Runtime.InteropServices.Marshal.ReleaseComObject.
#2 Dispose VS. null

Mydataset.dispose();
or
Mydatatset =null

If there is a Dispose() method available, call it when you are done.
#3 c# class objects
myobject Myinstance = new myobject();
...
..
Myinstance =null <--correct ?

If it's a local variable, it's pointless. The GC detects when an object is
no longer reachable and will collect it when it runs it's collection.
However, if it's a field of an other object that you will keep, but don't
need that field, setting it to null will help (my making it unreachable).
Same goes for static fields -- since they are always reachable you need to
explicitly set them to null when done.

-mike
MVP
 
Anthony,

See inline:

Anthony leong kar keong said:
Hi i would like to ensure some code cleaning methods.

#1 COM objects
Is the following the correct way ?
objMe = null;

If you are using a COM object represented in .NET, then you should be
calling the static ReleaseComObject on the Marshal class, passing in the
object. Then, you should set the reference to null. In general, in .NET,
setting the reference to null will make the object eligible for garbage
collection. When that GC happens is up to you, but the sooner you release
it, the sooner it is eligible. However, unmanaged resources that are held
onto by the object are not freed. In order to do that, for this case, you
should call the ReleaseComObject method.
#2 Dispose VS. null

Mydataset.dispose();
or
Mydatatset =null

Dispose and null are not competing methods. Dispose is the way of
indicating that resources the object represents/holds should be disposed of
in a timely manner, instead of waiting for a GC. So, what you should do is
call Dispose on the object, and then set the reference to null.
#3 c# class objects
myobject Myinstance = new myobject();
...
..
Myinstance =null <--correct ?

If you are done with it, then yes. However, if it implements
IDisposable, then you might want to call Dispose on that as well.

Hope this helps.

i
 
Does setting a local to null help? I thought that the CLR was advanced
enough to realise when a local was no longer used and thus referring to it
later had no benefit (And possibly a detriment)? (This is my understanding
after reading about 10 posts like this).
-mike
MVP
 
Mike,

You are right. I had forgotten some of the conversations in previous
threads. I know that C# does it (for release builds), but I don't know
about other languages.
 
Nicholas Paldino said:
Mike,

You are right. I had forgotten some of the conversations in previous
threads. I know that C# does it (for release builds), but I don't know
about other languages.
Nicholas,
This is not language dependent, it's the Jitter that verifies that an object can no longer be accessed.
Note that the Jitter doesn't track the lifetime all variables in very large methods, so, if you need to be sure that an object "can"
be collected before the reference goes out of scope, you must set it to null.

Willy.
 
Back
Top