Destruction of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Søndergaard

    #1

    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


  • Michael Søndergaard

    #2
    Re: Destruction of objects

    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]" <nicholas.paldi no@exisconsulti ng.com> wrote
    in message news:uFVnmbUVDH A.2272@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Michael,
    >
    > When you do this:
    >
    > D.A = null;
    >
    > You are just setting the reference that the instance of D is holding[/color]
    to[color=blue]
    > A. It does not delete the object.
    >
    > The list itself still has a reference to A.[/color]


    Comment

    • Scott

      #3
      Re: Destruction of objects

      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:vibtqqf2lk 713f@corp.super news.com...[color=blue]
      > "Michael Søndergaard" <nospam@nospam. com> wrote in message
      > news:O20T3xUVDH A.484@TK2MSFTNG P09.phx.gbl...[color=green]
      > > Thanks alot, that cleared some things, but then you might answer[/color][/color]
      me[color=blue]
      > this.[color=green]
      > >
      > > If D reference A and I set A = null, would that mean D becomes[/color][/color]
      null[color=blue]
      > or A[color=green]
      > > becomes null and D points to the original A, which doesn't get GC?[/color]
      >
      > 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[/color]
      it).[color=blue]
      > 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[/color]
      have[color=blue]
      > pins (instance variables) by themselves. Balloons can be attached to
      > multiple pins at the same time (objects can be referenced by more[/color]
      than[color=blue]
      > 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[/color]
      balloon[color=blue]
      > is
      > cut (the last reference is overwritten with another value) the[/color]
      balloon[color=blue]
      > floats up into the sky and after a while, it gets recycled by a[/color]
      small[color=blue]
      > plane sweeping the area (unreferenced objects are cleaned up by the
      > garbage collector, but not immediately). This even works for a[/color]
      network[color=blue]
      > of
      > balloons (objects referencing each other): Once the last thread to a
      > bunch
      > of balloons is cut, up it goes.
      > </quote>
      >
      >
      >
      >[/color]


      Comment

      Working...