Destroying objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Microsoft

    #1

    Destroying objects

    I am working on a VB.NET (2003) application and I am confused about object
    lifetimes. I have a class of objects that are created in a manager class and
    added to a collection in that class. I am not sure how to destroy an object
    once it is no longer required as a part of the collection. In VB 6 I would
    have assigned "nothing" to it. According to my extensive reading this is not
    the way to go with VB.NET.

    My objects are derived from "Object" as is my class that has the collection.
    Will the objects be destroyed when the ref-count goes to zero?

    All the objects only have managed resources allocated, so do I need to
    implement a Dispose method?

    Many thanks,
    Sid.


  • Armin Zingler

    #2
    Re: Destroying objects

    "Microsoft" <sid@nowhere.co m> schrieb[color=blue]
    > I am working on a VB.NET (2003) application and I am confused about
    > object lifetimes. I have a class of objects that are created in a[/color]

    What is a "class of objects"?
    [color=blue]
    > manager class and added to a collection in that class. I am not sure
    > how to destroy an object once it is no longer required as a part of
    > the collection. In VB 6 I would have assigned "nothing" to it.[/color]

    In VB6 I would remove it from the collection. I'd do the same in VB.Net.
    [color=blue]
    > According to my extensive reading this is not the way to go with
    > VB.NET.[/color]

    Why not?
    [color=blue]
    > My objects are derived from "Object" as is my class that has the
    > collection. Will the objects be destroyed when the ref-count goes to
    > zero?[/color]

    There is no ref count. The garbage collector destroys all objects when they
    are not reachable anymore, and when it "thinks" there is the time to do it
    or whenever it is necessary.

    [color=blue]
    > All the objects only have managed resources allocated, so do I need
    > to implement a Dispose method?[/color]

    No, unless the object created other objects that have a Dispose method and
    holds references to them.


    It's all explained here:
    http://msdn.microsoft.com/library/en...management.asp

    http://msdn.microsoft.com/library/en...collection.asp


    Armin

    Comment

    • Phill  W.

      #3
      Re: Destroying objects


      "Microsoft" <sid@nowhere.co m> wrote in message
      news:%23tJU4CTT GHA.5884@TK2MSF TNGP14.phx.gbl. ..
      [color=blue]
      > My objects are derived from "Object" as is my class that has the
      > collection. Will the objects be destroyed when the ref-count goes to zero?[/color]

      No, they'll be destroyed when nothing is referencing them and Garbage
      Collection can be bothered to get around to cleaning them up.
      [color=blue]
      > All the objects only have managed resources allocated, so do I need to
      > implement a Dispose method?[/color]

      Probably not, no, but it's worth checking whether any of the classes
      you're /using/ have a Dispose method - if so, you ought to call it (from
      your
      own).

      HTH,
      Phill W.


      Comment

      • Sid Price

        #4
        Re: Destroying objects

        Thank you for the input,
        Sid.

        "Phill W." <p-.a-.w-a-r-d@o-p-e-n.a-c.u-k> wrote in message
        news:dvr884$hv0 $1@yarrow.open. ac.uk...[color=blue]
        >
        > "Microsoft" <sid@nowhere.co m> wrote in message
        > news:%23tJU4CTT GHA.5884@TK2MSF TNGP14.phx.gbl. ..
        >[color=green]
        >> My objects are derived from "Object" as is my class that has the
        >> collection. Will the objects be destroyed when the ref-count goes to
        >> zero?[/color]
        >
        > No, they'll be destroyed when nothing is referencing them and Garbage
        > Collection can be bothered to get around to cleaning them up.
        >[color=green]
        >> All the objects only have managed resources allocated, so do I need to
        >> implement a Dispose method?[/color]
        >
        > Probably not, no, but it's worth checking whether any of the classes
        > you're /using/ have a Dispose method - if so, you ought to call it (from
        > your
        > own).
        >
        > HTH,
        > Phill W.
        >
        >[/color]


        Comment

        Working...