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

Destruction of objects

I can't seem to figure this one out.

I have a list of objects containing objects like A, B, C. I also have
another object D which referer object A.
At some point in my code I need to delete the reference to object A in obj
D, so I do this D.A = null;

The question is does this only delete the reference to A or does it also
delete the object by invoking the destructor of object A. Or am I saved by
the fact, that a reference still exists to object A, because it is contained
in the list object.

Thanks
Michael Søndergaard
Nov 15 '05 #1
2 5523
Thanks alot, that cleared some things, but then you might answer me this.

If D reference A and I set A = null, would that mean D becomes null or A
becomes null and D points to the original A, which doesn't get GC?

Michael Søndergaard
"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:uF**************@TK2MSFTNGP11.phx.gbl...
Michael,

When you do this:

D.A = null;

You are just setting the reference that the instance of D is holding to A. It does not delete the object.

The list itself still has a reference to A.

Nov 15 '05 #2
Michael Mayer,
Thanks for the balloon analogy. I have never heard it before, and
even though I kinda understood the gc, this makes it a bit, well,
cooler.

Scott

"Michael Mayer" <mrmayer at charter dot net> wrote in message
news:vi************@corp.supernews.com...
"Michael Søndergaard" <no****@nospam.com> wrote in message
news:O2*************@TK2MSFTNGP09.phx.gbl...
Thanks alot, that cleared some things, but then you might answer
me this.

If D reference A and I set A = null, would that mean D becomes
null or A
becomes null and D points to the original A, which doesn't get GC?
I think you have a typo or two in the above. Anyway, two different
variables can point to the same object (as you know). Changing the
reference of one variable (even to null it out) doesn't affect the
other variable. It will still point to the object.

Think about the balloon analogy (not sure if you're familiar with

it). from http://tinyurl.com/icvx

<quote>
I always loved the analogy of balloons (objects) held by threads
(references) attached to pins (variables). Balloons (objects) can have pins (instance variables) by themselves. Balloons can be attached to
multiple pins at the same time (objects can be referenced by more than one
variables), but one pin (variable) only ever holds one balloon
(object).
Attaching a new balloon to a pin (assigning to a variable) cuts the
thread
to the balloon previously held there. If the last thread to a balloon is
cut (the last reference is overwritten with another value) the balloon floats up into the sky and after a while, it gets recycled by a small plane sweeping the area (unreferenced objects are cleaned up by the
garbage collector, but not immediately). This even works for a network of
balloons (objects referencing each other): Once the last thread to a
bunch
of balloons is cut, up it goes.
</quote>

Nov 15 '05 #3

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

Similar topics

4
by: johny smith | last post by:
Suppose there is a policy that all objects are statically declared. For example: static Car car(); Then, is there a reason to have a destructor defined for the class Car. It would seem...
0
by: Stephan Keil | last post by:
Hi all, consider a class class X { static X& OneX() { static X sgl1; return sgl1; } static X& TheX() { static X sgl2(OneX()); return sgl2; } }; and two source files with global variables
3
by: owatona | last post by:
Hi to all, I would like to know how .net manage the objects and what are the cicle of life of the objects... Are the same this cicle, for the objects created in windows applications and for the...
4
by: DKode | last post by:
This should be an easy question. With all of my dataadapters, when I am done with them i do the following: DataAdapter da = new DataAdapter(); // do something with da da.Dispose(); Is...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
5
by: Parapura Rajkumar | last post by:
hey all class A { }; class B {
5
by: cctv.star | last post by:
I need to maintain certain data structure, shared by all instances of a class, declared as . This data structure must change whenever a new object is created or an existing object is destroyed. So...
6
by: Fred Mangusta | last post by:
Hi, I have an object, Model, that creates another object, Trainer. Trainer, given some data, fills some structures with processed data. My problem is the following: I need to pass those...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.