473,320 Members | 2,180 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.

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 1662
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: 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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
1
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....

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.