473,396 Members | 2,033 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,396 software developers and data experts.

Code Cleaning

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
Nov 15 '05 #1
5 1751
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.ReleaseComO bject.
#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
very thank you and sharing is very much appreciated

Nov 15 '05 #2
Anthony,

See inline:

"Anthony leong kar keong" <an*****@isys.com.my> wrote in message
news:0b****************************@phx.gbl...
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


very thank you and sharing is very much appreciated

Nov 15 '05 #3
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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:el**************@TK2MSFTNGP10.phx.gbl...
Anthony,

See inline:

"Anthony leong kar keong" <an*****@isys.com.my> wrote in message
news:0b****************************@phx.gbl...
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


very thank you and sharing is very much appreciated


Nov 15 '05 #4
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 [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Giagnocavo [MVP]" <mg*******@Atrevido.net> wrote in message
news:uF**************@TK2MSFTNGP10.phx.gbl...
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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:el**************@TK2MSFTNGP10.phx.gbl...
Anthony,

See inline:

"Anthony leong kar keong" <an*****@isys.com.my> wrote in message
news:0b****************************@phx.gbl...
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


very thank you and sharing is very much appreciated



Nov 15 '05 #5

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:OJ*************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

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.
Nov 15 '05 #6

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

Similar topics

1
by: StinkFinger | last post by:
Hello all, After enabling sessions for my website I have approx. 785 files in my sessiondata folder. Now, I know what these are, however, I don't know when/how is the best way to clean them...
2
by: Ellen K. | last post by:
What tools has everyone used for cleaning name and address data (including identifying not-immediately-obvious duplicates) in connection with a CRM project or the Customer dimension of a data...
1
by: Matej Cepl | last post by:
Hi, can anybody help me with the cleaning of really messy HTML from the news site into really clean XHTML, which I would like to then analyze with some qualitative analysis (probably exporting...
6
by: Dean Ware | last post by:
Hi, I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0. I am now at the stage where I wish to start tidying up my code. I have lots of...
3
by: Pierre Saint-Jacques | last post by:
DB2 V8.2 has a new envir. var. DB2_USE_ALTERNATE_PAGE_CLEANING=YES The docs. mention that this will make DB2 ignore chngpgs_thresh and use softmax to even the rate of writing out of the bp's. ...
13
by: jadionne | last post by:
I have a new instalation that I am ironing out the wrinkles in. In the process I have had a few crashes and now my SQLlib\DB2 Dir is full of ..0 and .000 files. What is the best way to clean up...
11
by: junky_fellow | last post by:
What are the basic guidelines to write an optimised code. What points should one keep in mind for this ? Is this purely architecture or complier specific ? Are there any general techniques that...
1
by: Mikey | last post by:
Do NativeWindows get destroyed when the app shuts down? While my app is running I can Spy++ and see my NativeWindow handle. After app shutdown it's no longer there. But this is confusing...
0
by: Now You Know | last post by:
Carpet Cleaners Los Angeles Home Carpet Rug Upholstery Cleaning Phone 1 310 925 1720 OR 1-818-386-1022 Local Call California Wide We offer carpet cleaning services such as; Steam Cleaning, Dry...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.