Destroying objects.

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

    #1

    Destroying objects.

    Hello,

    I have multiple references to an object. Is it possible to destroy the
    object using one of the references, and setting the rest of them to Nothing?

    Dim objA as New A
    Dim objAR As A
    objAR = objA
    objA = Nothing
    REM Now the object is still alive.

    Dim objA as New A
    Dim objAR As A
    KILL(objA)
    If objAR is Nothing then blnGreat = True

    Thanks.


  • qwert

    #2
    Re: Destroying objects.

    What I meant was:

    Dim objA as New A
    Dim objAR As A
    objAR = objA
    KILL(objA)
    If objAR is Nothing then blnGreat = True




    Comment

    • Brian Gideon

      #3
      Re: Destroying objects.

      No. That's not possible. You have very little control over when an
      object is actually destroyed. That's the job of the garbage collector.
      What you do have complete control over is how many references there
      are to an object.

      If you're wanting deterministric finalization then use the IDisposable
      interface and adhere to the associated design pattern.

      If you're wanting to use advanced techniques for controlling the
      lifetime of objects you can use the WeakReference class.

      Can you explain a little more about what you're wanting to accomplish?

      Brian

      qwert wrote:[color=blue]
      > What I meant was:
      >
      > Dim objA as New A
      > Dim objAR As A
      > objAR = objA
      > KILL(objA)
      > If objAR is Nothing then blnGreat = True[/color]

      Comment

      • qwert

        #4
        Re: Destroying objects.

        I want to set all references to an object to Nothing in one statement.
        Instead of setting X references to Nothing, I would like to call one
        statement with one of the X references, setting them all to Nothing.

        I'm not interested in when the object is destroyed. As long as all the
        references are Nothing so the garbage collector knows it's good to go.



        "Brian Gideon" <briangideon@ya hoo.com> wrote in message
        news:1140709793 .609331.257730@ g44g2000cwa.goo glegroups.com.. .[color=blue]
        > No. That's not possible. You have very little control over when an
        > object is actually destroyed. That's the job of the garbage collector.
        > What you do have complete control over is how many references there
        > are to an object.
        >
        > If you're wanting deterministric finalization then use the IDisposable
        > interface and adhere to the associated design pattern.
        >
        > If you're wanting to use advanced techniques for controlling the
        > lifetime of objects you can use the WeakReference class.
        >
        > Can you explain a little more about what you're wanting to accomplish?
        >
        > Brian
        >
        > qwert wrote:[color=green]
        > > What I meant was:
        > >
        > > Dim objA as New A
        > > Dim objAR As A
        > > objAR = objA
        > > KILL(objA)
        > > If objAR is Nothing then blnGreat = True[/color]
        >[/color]


        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Destroying objects.

          Qwert

          As you do this, then
          Dim objA as New A
          'A new object A is created on the stack the reference to it is objA

          Dim objAR As A
          'A placeholder is created on the stack to hold a reference to an object A

          objAR = objA
          'There is told that the placeholder from objAr is not pointing as well to
          the objA

          KILL(objA)
          'This instruction does not exist if you would see this as
          OjbA = Nothing than the only thing that happens that the placeholder for the
          reference in the stack is set to Nothing (probably physical null or
          something).

          If objAR is Nothing then blnGreat = True
          'the object itself will still exist until that there is no reference anymore
          to it from any place, even the places that you did not set yourself.

          To give another example
          Dim A as new classA
          Dim A = new classA

          Here are two objects created on the managed heap.
          The memorey of the first one will direct as the Garbage Collector start be
          released.

          And as advice don't take to much care of releasing objects as long as there
          are no problems. Used memory is never a problem as long as there is enough.
          Some strange behaviour is at some developpers that it has forever to be as
          low as possible.

          I hope this gives an idea.

          Cor


          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Destroying objects.

            Duh and typo and a little addition.[color=blue]
            >
            > objAR = objA
            > 'There is told that the placeholder from objAr is not pointing as well to
            > the objA[/color]
            *now* pointing as well to objA
            [color=blue]
            > If objAR is Nothing then blnGreat = True
            > 'the object itself will still exist until that there is no reference
            > anymore to it from any place, even the places that you did not set
            > yourself.
            >[/color]
            So in this case when there is not any other code extra
            objA = nothing

            Be aware that this has only sense with globaly created objects. The others
            goes direct out of scope if there is not pointing indirectly something to
            it. (By instance because it is added to a collection as by instance a
            control to a form).

            Cor


            Comment

            • Brian Gideon

              #7
              Re: Destroying objects.

              qwert wrote:[color=blue]
              > I want to set all references to an object to Nothing in one statement.
              > Instead of setting X references to Nothing, I would like to call one
              > statement with one of the X references, setting them all to Nothing.
              >[/color]

              That's not possible either and for a good reason. It would be nearly
              impossible to write stable code if a reference you declare and
              initialize could be null at anytime.
              [color=blue]
              > I'm not interested in when the object is destroyed. As long as all the
              > references are Nothing so the garbage collector knows it's good to go.
              >[/color]

              If you want all references to equal Nothing then go through each one
              and set them to Nothing. That's the only way to do it. Maybe I'm not
              understanding what it is you're wanting to accomplish. Can you explain
              why you believe you can't set each reference to Nothing?

              Comment

              • qwert

                #8
                Re: Destroying objects.

                I can set each reference to Nothing. It's not really a technical problem. I
                wondered what if a third party uses the assembly, and (s)he is a bit sloppy,
                and doesn't clean up as supposed to. Call this one function and away it
                goes.

                Thanks for the responses.



                "Brian Gideon" <briangideon@ya hoo.com> wrote in message
                news:1140712866 .775327.158570@ u72g2000cwu.goo glegroups.com.. .[color=blue]
                > qwert wrote:[color=green]
                > > I want to set all references to an object to Nothing in one statement.
                > > Instead of setting X references to Nothing, I would like to call one
                > > statement with one of the X references, setting them all to Nothing.
                > >[/color]
                >
                > That's not possible either and for a good reason. It would be nearly
                > impossible to write stable code if a reference you declare and
                > initialize could be null at anytime.
                >[color=green]
                > > I'm not interested in when the object is destroyed. As long as all the
                > > references are Nothing so the garbage collector knows it's good to go.
                > >[/color]
                >
                > If you want all references to equal Nothing then go through each one
                > and set them to Nothing. That's the only way to do it. Maybe I'm not
                > understanding what it is you're wanting to accomplish. Can you explain
                > why you believe you can't set each reference to Nothing?
                >[/color]


                Comment

                • Brian Gideon

                  #9
                  Re: Destroying objects.

                  Okay, this is making more sense now. There's not much you can do to
                  completely eliminate the possibility of a 3rd party using your assembly
                  incorrectly. A well designed library can mitigate the problem to some
                  extent, but if a developer throws your object into a Hashtable or
                  something and forgets about then that isn't your fault.

                  Brian

                  qwert wrote:[color=blue]
                  > I can set each reference to Nothing. It's not really a technical problem. I
                  > wondered what if a third party uses the assembly, and (s)he is a bit sloppy,
                  > and doesn't clean up as supposed to. Call this one function and away it
                  > goes.
                  >
                  > Thanks for the responses.
                  >
                  >[/color]

                  Comment

                  • CMM

                    #10
                    Re: Destroying objects.

                    Not sure I understand. You can't control who "references " your object and
                    you can't control when the Garbage Collector destroys it (which it won't do
                    if there is anybody "referencin g" it).
                    But, you sure can control whether your object kills itself (it refuses to do
                    work). Implement IDisposable. Call Dispose on the object... keep a private
                    variable stating in the object that states it considers itself disposed and
                    put in a conditional checks into each property and method accessed in the
                    object.

                    Public MyClass
                    Implements IDisposable
                    ...
                    Public Sub DoSomething()
                    If Not m_disposed Then
                    'do your work
                    Else
                    Throw New Exception("nuh uh, you can't access me.")
                    End If
                    End Sub

                    Public Property Something
                    Get
                    If Not m_disposed Then
                    Return m_something
                    Else
                    Throw New Exception("nuh uh, you can't access me.")
                    End If
                    End Get
                    ....


                    --
                    -C. Moya

                    "qwert" <nospam@nospam. nl> wrote in message
                    news:g5GdnQMyF-qNQmDeRVny1Q@ca sema.nl...[color=blue]
                    >I want to set all references to an object to Nothing in one statement.
                    > Instead of setting X references to Nothing, I would like to call one
                    > statement with one of the X references, setting them all to Nothing.
                    >
                    > I'm not interested in when the object is destroyed. As long as all the
                    > references are Nothing so the garbage collector knows it's good to go.
                    >
                    >
                    >
                    > "Brian Gideon" <briangideon@ya hoo.com> wrote in message
                    > news:1140709793 .609331.257730@ g44g2000cwa.goo glegroups.com.. .[color=green]
                    >> No. That's not possible. You have very little control over when an
                    >> object is actually destroyed. That's the job of the garbage collector.
                    >> What you do have complete control over is how many references there
                    >> are to an object.
                    >>
                    >> If you're wanting deterministric finalization then use the IDisposable
                    >> interface and adhere to the associated design pattern.
                    >>
                    >> If you're wanting to use advanced techniques for controlling the
                    >> lifetime of objects you can use the WeakReference class.
                    >>
                    >> Can you explain a little more about what you're wanting to accomplish?
                    >>
                    >> Brian
                    >>
                    >> qwert wrote:[color=darkred]
                    >> > What I meant was:
                    >> >
                    >> > Dim objA as New A
                    >> > Dim objAR As A
                    >> > objAR = objA
                    >> > KILL(objA)
                    >> > If objAR is Nothing then blnGreat = True[/color]
                    >>[/color]
                    >
                    >[/color]


                    Comment

                    • CMM

                      #11
                      Re: Destroying objects.

                      You don't even have to do the conditional. You can put an Assert at the top
                      of the method property code which will terminate the method immediately if
                      m_disposed.

                      --
                      -C. Moya

                      "CMM" <cmm@nospam.com > wrote in message
                      news:eehL88MOGH A.3264@TK2MSFTN GP11.phx.gbl...[color=blue]
                      > Not sure I understand. You can't control who "references " your object and
                      > you can't control when the Garbage Collector destroys it (which it won't
                      > do if there is anybody "referencin g" it).
                      > But, you sure can control whether your object kills itself (it refuses to
                      > do work). Implement IDisposable. Call Dispose on the object... keep a
                      > private variable stating in the object that states it considers itself
                      > disposed and put in a conditional checks into each property and method
                      > accessed in the object.
                      >
                      > Public MyClass
                      > Implements IDisposable
                      > ...
                      > Public Sub DoSomething()
                      > If Not m_disposed Then
                      > 'do your work
                      > Else
                      > Throw New Exception("nuh uh, you can't access me.")
                      > End If
                      > End Sub
                      >
                      > Public Property Something
                      > Get
                      > If Not m_disposed Then
                      > Return m_something
                      > Else
                      > Throw New Exception("nuh uh, you can't access me.")
                      > End If
                      > End Get
                      > ...
                      >
                      >
                      > --
                      > -C. Moya
                      > www.cmoya.com
                      > "qwert" <nospam@nospam. nl> wrote in message
                      > news:g5GdnQMyF-qNQmDeRVny1Q@ca sema.nl...[color=green]
                      >>I want to set all references to an object to Nothing in one statement.
                      >> Instead of setting X references to Nothing, I would like to call one
                      >> statement with one of the X references, setting them all to Nothing.
                      >>
                      >> I'm not interested in when the object is destroyed. As long as all the
                      >> references are Nothing so the garbage collector knows it's good to go.
                      >>
                      >>
                      >>
                      >> "Brian Gideon" <briangideon@ya hoo.com> wrote in message
                      >> news:1140709793 .609331.257730@ g44g2000cwa.goo glegroups.com.. .[color=darkred]
                      >>> No. That's not possible. You have very little control over when an
                      >>> object is actually destroyed. That's the job of the garbage collector.
                      >>> What you do have complete control over is how many references there
                      >>> are to an object.
                      >>>
                      >>> If you're wanting deterministric finalization then use the IDisposable
                      >>> interface and adhere to the associated design pattern.
                      >>>
                      >>> If you're wanting to use advanced techniques for controlling the
                      >>> lifetime of objects you can use the WeakReference class.
                      >>>
                      >>> Can you explain a little more about what you're wanting to accomplish?
                      >>>
                      >>> Brian
                      >>>
                      >>> qwert wrote:
                      >>> > What I meant was:
                      >>> >
                      >>> > Dim objA as New A
                      >>> > Dim objAR As A
                      >>> > objAR = objA
                      >>> > KILL(objA)
                      >>> > If objAR is Nothing then blnGreat = True
                      >>>[/color]
                      >>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Brian Gideon

                        #12
                        Re: Destroying objects.

                        I usually put all of my argument and state validation code at the top
                        of the methods in individual if statements. An ObjectDisposedE xception
                        should be thrown if that check fails. The problem with Assert is that
                        it won't make it into the release build.

                        I try to avoid throwing exceptions from properties since callers don't
                        expect them in that case.

                        CMM wrote:[color=blue]
                        > You don't even have to do the conditional. You can put an Assert at the top
                        > of the method property code which will terminate the method immediately if
                        > m_disposed.
                        >
                        > --
                        > -C. Moya
                        > www.cmoya.com
                        > "CMM" <cmm@nospam.com > wrote in message
                        > news:eehL88MOGH A.3264@TK2MSFTN GP11.phx.gbl...[color=green]
                        > > Not sure I understand. You can't control who "references " your object and
                        > > you can't control when the Garbage Collector destroys it (which it won't
                        > > do if there is anybody "referencin g" it).
                        > > But, you sure can control whether your object kills itself (it refuses to
                        > > do work). Implement IDisposable. Call Dispose on the object... keep a
                        > > private variable stating in the object that states it considers itself
                        > > disposed and put in a conditional checks into each property and method
                        > > accessed in the object.
                        > >
                        > > Public MyClass
                        > > Implements IDisposable
                        > > ...
                        > > Public Sub DoSomething()
                        > > If Not m_disposed Then
                        > > 'do your work
                        > > Else
                        > > Throw New Exception("nuh uh, you can't access me.")
                        > > End If
                        > > End Sub
                        > >
                        > > Public Property Something
                        > > Get
                        > > If Not m_disposed Then
                        > > Return m_something
                        > > Else
                        > > Throw New Exception("nuh uh, you can't access me.")
                        > > End If
                        > > End Get
                        > > ...
                        > >
                        > >
                        > > --
                        > > -C. Moya
                        > > www.cmoya.com
                        > > "qwert" <nospam@nospam. nl> wrote in message
                        > > news:g5GdnQMyF-qNQmDeRVny1Q@ca sema.nl...[/color][/color]

                        Comment

                        • CMM

                          #13
                          Re: Destroying objects.

                          Trace.Assert does make it into Release AFAIK (you're thinking of
                          Debug.Assert).
                          But, anyhow, I meant more your own *encapsulated* exception thrower rather
                          than littering conditionals in all your methods.... which is ugly and hard
                          to maintain.

                          DisposedAssert( ) 'called at the top of your methods

                          Private Sub DisposedAssert( )
                          If m_disposed Then
                          Throw New ObjectDisposedE xception
                          Endif
                          End Sub

                          --
                          -C. Moya

                          "Brian Gideon" <briangideon@ya hoo.com> wrote in message
                          news:1140747301 .914184.102020@ v46g2000cwv.goo glegroups.com.. .[color=blue]
                          >I usually put all of my argument and state validation code at the top
                          > of the methods in individual if statements. An ObjectDisposedE xception
                          > should be thrown if that check fails. The problem with Assert is that
                          > it won't make it into the release build.
                          >
                          > I try to avoid throwing exceptions from properties since callers don't
                          > expect them in that case.
                          >
                          > CMM wrote:[color=green]
                          >> You don't even have to do the conditional. You can put an Assert at the
                          >> top
                          >> of the method property code which will terminate the method immediately
                          >> if
                          >> m_disposed.
                          >>
                          >> --
                          >> -C. Moya
                          >> www.cmoya.com
                          >> "CMM" <cmm@nospam.com > wrote in message
                          >> news:eehL88MOGH A.3264@TK2MSFTN GP11.phx.gbl...[color=darkred]
                          >> > Not sure I understand. You can't control who "references " your object
                          >> > and
                          >> > you can't control when the Garbage Collector destroys it (which it
                          >> > won't
                          >> > do if there is anybody "referencin g" it).
                          >> > But, you sure can control whether your object kills itself (it refuses
                          >> > to
                          >> > do work). Implement IDisposable. Call Dispose on the object... keep a
                          >> > private variable stating in the object that states it considers itself
                          >> > disposed and put in a conditional checks into each property and method
                          >> > accessed in the object.
                          >> >
                          >> > Public MyClass
                          >> > Implements IDisposable
                          >> > ...
                          >> > Public Sub DoSomething()
                          >> > If Not m_disposed Then
                          >> > 'do your work
                          >> > Else
                          >> > Throw New Exception("nuh uh, you can't access me.")
                          >> > End If
                          >> > End Sub
                          >> >
                          >> > Public Property Something
                          >> > Get
                          >> > If Not m_disposed Then
                          >> > Return m_something
                          >> > Else
                          >> > Throw New Exception("nuh uh, you can't access me.")
                          >> > End If
                          >> > End Get
                          >> > ...
                          >> >
                          >> >
                          >> > --
                          >> > -C. Moya
                          >> > www.cmoya.com
                          >> > "qwert" <nospam@nospam. nl> wrote in message
                          >> > news:g5GdnQMyF-qNQmDeRVny1Q@ca sema.nl...[/color][/color]
                          >[/color]


                          Comment

                          • Brian Gideon

                            #14
                            Re: Destroying objects.

                            Ah yes. I was thinking of Debug.Assert. Trace.Assert does indeed get
                            included in release builds. Well, that is as long as TRACE is defined.
                            Either way, it's still not an appropriate strategy since they won't be
                            throwing the ObjectDisposedE xception. But, that's all moot since I
                            realize now what you were talking about :)

                            And yes. I see what you're talking about with moving the checks into
                            separate methods. It does make things easier.

                            CMM wrote:[color=blue]
                            > Trace.Assert does make it into Release AFAIK (you're thinking of
                            > Debug.Assert).
                            > But, anyhow, I meant more your own *encapsulated* exception thrower rather
                            > than littering conditionals in all your methods.... which is ugly and hard
                            > to maintain.
                            >
                            > DisposedAssert( ) 'called at the top of your methods
                            >
                            > Private Sub DisposedAssert( )
                            > If m_disposed Then
                            > Throw New ObjectDisposedE xception
                            > Endif
                            > End Sub
                            >
                            > --
                            > -C. Moya
                            > www.cmoya.com[/color]

                            Comment

                            Working...