473,385 Members | 1,472 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,385 software developers and data experts.

Basic Question

How do you force destruction of an object?

Will a simple "someObj = null" suffice?

-----
MyType someObj = new MyType();
...
...
someObj = null; // <= Will this call the destructor?

-----

--

Happy Hacking,
Gaurav
http://gvaish.virtualave.net
--------------------------------
Jul 21 '05 #1
4 1664
Gaurav Vaish <me@privacy.net> wrote:
How do you force destruction of an object?
You don't.
Will a simple "someObj = null" suffice?
No:

1) There may be other references to the object
2) The object won't be garbage collected until the garbage collector
next collects the generation the object lives in.
-----
MyType someObj = new MyType();
...
...
someObj = null; // <= Will this call the destructor?


No.

--
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
If there are no other instances of the object then this should suffice.. am
I right?

--

Happy Hacking,
Gaurav
http://gvaish.virtualave.net
--------------------------------
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Gaurav Vaish <me@privacy.net> wrote:
How do you force destruction of an object?


You don't.
Will a simple "someObj = null" suffice?


No:

1) There may be other references to the object
2) The object won't be garbage collected until the garbage collector
next collects the generation the object lives in.
-----
MyType someObj = new MyType();
...
...
someObj = null; // <= Will this call the destructor?


No.

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


Jul 21 '05 #3
Gaurav Vaish <me@privacy.net> wrote:
If there are no other instances of the object then this should suffice.. am
I right?


No. "No other instances of the object" doesn't make sense - an object
*is* an instance. Did you mean if there are no other references to the
object? If so, that's enough to make it eligible for garbage collection
- but it doesn't mean that it will instantly be garbage collected, or
that the finalizer will immediately be called.

Also note that if it's a local variable, or if it's an instance field
of another object which is going to become eligible for garbage
collection, you don't need to set it to null.

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

No, "someObj = null" will not suffice. .NET uses reference tracking garbage
collection which means that your object will be destroyed some time from the
point the last reference to it is released and the application terminates.

Here's a link to a great article that describes this process in detail.

http://www.fawcette.com/vsm/2002_12/...na/default.asp

--
Rob Windsor
G6 Consulting
Toronto, Canada

"Gaurav Vaish" <me@privacy.net> wrote in message
news:bk************@ID-108900.news.uni-berlin.de...
How do you force destruction of an object?

Will a simple "someObj = null" suffice?

-----
MyType someObj = new MyType();
...
...
someObj = null; // <= Will this call the destructor?

-----

--

Happy Hacking,
Gaurav
http://gvaish.virtualave.net
--------------------------------

Jul 21 '05 #5

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

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.