Reference Counting

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

    Reference Counting

    Hello everyone,
    I am an old C++/COM programmer that converted about 8 months ago to
    C#/.Net, and I have no regrets.

    I am now converting our caching subsystem from COM to .Net and I have a
    little problem to resolve. Our old COM cache would cleanup "unreferenc ed"
    entities by checking the refcount on the entities in the cache. Whenever the
    refcount was equal to 2 (this means the cache had a reference and the
    cleanup had another) then we knew the entity was not referenced by the
    application and it would get "destroyed" .

    I undestand that .Net does not use refcounting, but I am confused as to
    how I could cleanup in .Net. Can someone point me in the rigth direction?

    Thank you!

    -MP.


  • Alex

    #2
    Re: Reference Counting

    Hi,
    I think you should take a look at this article:
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    I hope this helps.

    - Alexander Rauser



    MP wrote:[color=blue]
    > Hello everyone,
    > I am an old C++/COM programmer that converted about 8 months ago to
    > C#/.Net, and I have no regrets.
    >
    > I am now converting our caching subsystem from COM to .Net and I have a
    > little problem to resolve. Our old COM cache would cleanup "unreferenc ed"
    > entities by checking the refcount on the entities in the cache. Whenever the
    > refcount was equal to 2 (this means the cache had a reference and the
    > cleanup had another) then we knew the entity was not referenced by the
    > application and it would get "destroyed" .
    >
    > I undestand that .Net does not use refcounting, but I am confused as to
    > how I could cleanup in .Net. Can someone point me in the rigth direction?
    >
    > Thank you!
    >
    > -MP.
    >
    >[/color]

    Comment

    • MP

      #3
      Re: Reference Counting

      Thank you Alex, I will.

      -MP
      "Alex" <www.flashshop. info> wrote in message
      news:eKkEUyRDFH A.3648@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi,
      > I think you should take a look at this article:
      > http://msdn.microsoft.com/msdnmag/is...i/default.aspx
      >
      > I hope this helps.
      >
      > - Alexander Rauser
      >
      > http://www.flashshop.info
      >
      > MP wrote:[color=green]
      >> Hello everyone,
      >> I am an old C++/COM programmer that converted about 8 months ago to
      >> C#/.Net, and I have no regrets.
      >>
      >> I am now converting our caching subsystem from COM to .Net and I have
      >> a little problem to resolve. Our old COM cache would cleanup
      >> "unreferenc ed" entities by checking the refcount on the entities in the
      >> cache. Whenever the refcount was equal to 2 (this means the cache had a
      >> reference and the cleanup had another) then we knew the entity was not
      >> referenced by the application and it would get "destroyed" .
      >>
      >> I undestand that .Net does not use refcounting, but I am confused as
      >> to how I could cleanup in .Net. Can someone point me in the rigth
      >> direction?
      >>
      >> Thank you!
      >>
      >> -MP.[/color][/color]


      Comment

      • Sean Hederman

        #4
        Re: Reference Counting

        In addition to Alex's response, if you're going to be implementing your own
        caching system, have a look at the MSDN documentation for WeakReference.
        This allows you to hold references to items that can still be garbage
        collected, assuming no strong references to them exist. It has an IsAlive
        property to allow you to check if the object is still in memory. It's
        theoretically similar to having a COM pointer to an object without calling
        AddRef(). Since a garbage collection might only collect the object some time
        after all strong references to it are cleared, holding onto the weak
        reference acts as quite a nice caching mechanism.

        Of course, you'd probably be better off using an existing caching system
        (such as the Caching Application Block).

        "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
        news:ODTGtqRDFH A.392@TK2MSFTNG P14.phx.gbl...[color=blue]
        > Hello everyone,
        > I am an old C++/COM programmer that converted about 8 months ago to
        > C#/.Net, and I have no regrets.
        >
        > I am now converting our caching subsystem from COM to .Net and I have a
        > little problem to resolve. Our old COM cache would cleanup "unreferenc ed"
        > entities by checking the refcount on the entities in the cache. Whenever
        > the refcount was equal to 2 (this means the cache had a reference and the
        > cleanup had another) then we knew the entity was not referenced by the
        > application and it would get "destroyed" .
        >
        > I undestand that .Net does not use refcounting, but I am confused as to
        > how I could cleanup in .Net. Can someone point me in the rigth direction?
        >
        > Thank you!
        >
        > -MP.
        >[/color]


        Comment

        • robkit

          #5
          Re: Reference Counting

          CRL manages reference counting automatically. To make it call Release() all
          you need is to assign null to the COM object reference. It will mark the
          object for garbage collction. There is another possibility of calling
          Release() explicitly from the code like this:

          using System.Runtime. InteropServices ;

          IntPtr intptr = Marshal.GetIUnk nownForObject(c omObject);
          int count = Marshal.Release (intptr);




          "Sean Hederman" wrote:
          [color=blue]
          > In addition to Alex's response, if you're going to be implementing your own
          > caching system, have a look at the MSDN documentation for WeakReference.
          > This allows you to hold references to items that can still be garbage
          > collected, assuming no strong references to them exist. It has an IsAlive
          > property to allow you to check if the object is still in memory. It's
          > theoretically similar to having a COM pointer to an object without calling
          > AddRef(). Since a garbage collection might only collect the object some time
          > after all strong references to it are cleared, holding onto the weak
          > reference acts as quite a nice caching mechanism.
          >
          > Of course, you'd probably be better off using an existing caching system
          > (such as the Caching Application Block).
          >
          > "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
          > news:ODTGtqRDFH A.392@TK2MSFTNG P14.phx.gbl...[color=green]
          > > Hello everyone,
          > > I am an old C++/COM programmer that converted about 8 months ago to
          > > C#/.Net, and I have no regrets.
          > >
          > > I am now converting our caching subsystem from COM to .Net and I have a
          > > little problem to resolve. Our old COM cache would cleanup "unreferenc ed"
          > > entities by checking the refcount on the entities in the cache. Whenever
          > > the refcount was equal to 2 (this means the cache had a reference and the
          > > cleanup had another) then we knew the entity was not referenced by the
          > > application and it would get "destroyed" .
          > >
          > > I undestand that .Net does not use refcounting, but I am confused as to
          > > how I could cleanup in .Net. Can someone point me in the rigth direction?
          > >
          > > Thank you!
          > >
          > > -MP.
          > >[/color]
          >
          >
          >[/color]

          Comment

          • MP

            #6
            Re: Reference Counting

            Thank you Shaun,
            I will look at this as well, seems like the ideal solution.

            About the Caching Application Block, do you have some experience with it?
            I will read about it.

            Thank you !

            -MP

            "Sean Hederman" <usemy@blogentr y.com> wrote in message
            news:cu7tou$o42 $1@ctb-nnrp2.saix.net. ..[color=blue]
            > In addition to Alex's response, if you're going to be implementing your
            > own caching system, have a look at the MSDN documentation for
            > WeakReference. This allows you to hold references to items that can still
            > be garbage collected, assuming no strong references to them exist. It has
            > an IsAlive property to allow you to check if the object is still in
            > memory. It's theoretically similar to having a COM pointer to an object
            > without calling AddRef(). Since a garbage collection might only collect
            > the object some time after all strong references to it are cleared,
            > holding onto the weak reference acts as quite a nice caching mechanism.
            >
            > Of course, you'd probably be better off using an existing caching system
            > (such as the Caching Application Block).
            >
            > "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
            > news:ODTGtqRDFH A.392@TK2MSFTNG P14.phx.gbl...[color=green]
            >> Hello everyone,
            >> I am an old C++/COM programmer that converted about 8 months ago to
            >> C#/.Net, and I have no regrets.
            >>
            >> I am now converting our caching subsystem from COM to .Net and I have
            >> a little problem to resolve. Our old COM cache would cleanup
            >> "unreferenc ed" entities by checking the refcount on the entities in the
            >> cache. Whenever the refcount was equal to 2 (this means the cache had a
            >> reference and the cleanup had another) then we knew the entity was not
            >> referenced by the application and it would get "destroyed" .
            >>
            >> I undestand that .Net does not use refcounting, but I am confused as
            >> to how I could cleanup in .Net. Can someone point me in the rigth
            >> direction?
            >>
            >> Thank you!
            >>
            >> -MP.
            >>[/color]
            >
            >[/color]


            Comment

            • Wayne

              #7
              Re: Reference Counting

              Microsoft just released their new enterprise blocks. This is re-working (I
              believe) of the current blocks.



              Current blocks had lots of dependencies and were implemented differently
              across the blocks, the new enterprise blocks are suppose to give the blocks
              a more consistent feel across blocks. Also some of the dependencies were
              removed (again I believe this to be true)

              --
              Thanks
              Wayne Sepega
              Jacksonville, Fl


              "When a man sits with a pretty girl for an hour, it seems like a minute. But
              let him sit on a hot stove for a minute and it's longer than any hour.
              That's relativity." - Albert Einstein

              "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
              news:uzyBLFTDFH A.1932@TK2MSFTN GP14.phx.gbl...[color=blue]
              > Thank you Shaun,
              > I will look at this as well, seems like the ideal solution.
              >
              > About the Caching Application Block, do you have some experience with[/color]
              it?[color=blue]
              > I will read about it.
              >
              > Thank you !
              >
              > -MP
              >
              > "Sean Hederman" <usemy@blogentr y.com> wrote in message
              > news:cu7tou$o42 $1@ctb-nnrp2.saix.net. ..[color=green]
              > > In addition to Alex's response, if you're going to be implementing your
              > > own caching system, have a look at the MSDN documentation for
              > > WeakReference. This allows you to hold references to items that can[/color][/color]
              still[color=blue][color=green]
              > > be garbage collected, assuming no strong references to them exist. It[/color][/color]
              has[color=blue][color=green]
              > > an IsAlive property to allow you to check if the object is still in
              > > memory. It's theoretically similar to having a COM pointer to an object
              > > without calling AddRef(). Since a garbage collection might only collect
              > > the object some time after all strong references to it are cleared,
              > > holding onto the weak reference acts as quite a nice caching mechanism.
              > >
              > > Of course, you'd probably be better off using an existing caching system
              > > (such as the Caching Application Block).
              > >
              > > "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
              > > news:ODTGtqRDFH A.392@TK2MSFTNG P14.phx.gbl...[color=darkred]
              > >> Hello everyone,
              > >> I am an old C++/COM programmer that converted about 8 months ago to
              > >> C#/.Net, and I have no regrets.
              > >>
              > >> I am now converting our caching subsystem from COM to .Net and I[/color][/color][/color]
              have[color=blue][color=green][color=darkred]
              > >> a little problem to resolve. Our old COM cache would cleanup
              > >> "unreferenc ed" entities by checking the refcount on the entities in the
              > >> cache. Whenever the refcount was equal to 2 (this means the cache had a
              > >> reference and the cleanup had another) then we knew the entity was not
              > >> referenced by the application and it would get "destroyed" .
              > >>
              > >> I undestand that .Net does not use refcounting, but I am confused as
              > >> to how I could cleanup in .Net. Can someone point me in the rigth
              > >> direction?
              > >>
              > >> Thank you!
              > >>
              > >> -MP.
              > >>[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • MP

                #8
                Re: Reference Counting

                Thank you Wayne. I'll have a look at it.

                Have a nice day!
                "Wayne" <MeNotYou@commu nity.nospam> wrote in message
                news:efWT%232TD FHA.3416@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                > Microsoft just released their new enterprise blocks. This is re-working (I
                > believe) of the current blocks.
                >
                > http://msdn.microsoft.com/library/de...tml/entlib.asp
                >
                > Current blocks had lots of dependencies and were implemented differently
                > across the blocks, the new enterprise blocks are suppose to give the
                > blocks
                > a more consistent feel across blocks. Also some of the dependencies were
                > removed (again I believe this to be true)
                >
                > --
                > Thanks
                > Wayne Sepega
                > Jacksonville, Fl
                >
                >
                > "When a man sits with a pretty girl for an hour, it seems like a minute.
                > But
                > let him sit on a hot stove for a minute and it's longer than any hour.
                > That's relativity." - Albert Einstein
                >
                > "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
                > news:uzyBLFTDFH A.1932@TK2MSFTN GP14.phx.gbl...[color=green]
                >> Thank you Shaun,
                >> I will look at this as well, seems like the ideal solution.
                >>
                >> About the Caching Application Block, do you have some experience with[/color]
                > it?[color=green]
                >> I will read about it.
                >>
                >> Thank you !
                >>
                >> -MP
                >>
                >> "Sean Hederman" <usemy@blogentr y.com> wrote in message
                >> news:cu7tou$o42 $1@ctb-nnrp2.saix.net. ..[color=darkred]
                >> > In addition to Alex's response, if you're going to be implementing your
                >> > own caching system, have a look at the MSDN documentation for
                >> > WeakReference. This allows you to hold references to items that can[/color][/color]
                > still[color=green][color=darkred]
                >> > be garbage collected, assuming no strong references to them exist. It[/color][/color]
                > has[color=green][color=darkred]
                >> > an IsAlive property to allow you to check if the object is still in
                >> > memory. It's theoretically similar to having a COM pointer to an object
                >> > without calling AddRef(). Since a garbage collection might only collect
                >> > the object some time after all strong references to it are cleared,
                >> > holding onto the weak reference acts as quite a nice caching mechanism.
                >> >
                >> > Of course, you'd probably be better off using an existing caching
                >> > system
                >> > (such as the Caching Application Block).
                >> >
                >> > "MP" <martin.pare@I_ Will_Not_Give_Y ou_My_Address> wrote in message
                >> > news:ODTGtqRDFH A.392@TK2MSFTNG P14.phx.gbl...
                >> >> Hello everyone,
                >> >> I am an old C++/COM programmer that converted about 8 months ago to
                >> >> C#/.Net, and I have no regrets.
                >> >>
                >> >> I am now converting our caching subsystem from COM to .Net and I[/color][/color]
                > have[color=green][color=darkred]
                >> >> a little problem to resolve. Our old COM cache would cleanup
                >> >> "unreferenc ed" entities by checking the refcount on the entities in
                >> >> the
                >> >> cache. Whenever the refcount was equal to 2 (this means the cache had
                >> >> a
                >> >> reference and the cleanup had another) then we knew the entity was not
                >> >> referenced by the application and it would get "destroyed" .
                >> >>
                >> >> I undestand that .Net does not use refcounting, but I am confused
                >> >> as
                >> >> to how I could cleanup in .Net. Can someone point me in the rigth
                >> >> direction?
                >> >>
                >> >> Thank you!
                >> >>
                >> >> -MP.
                >> >>
                >> >
                >> >[/color]
                >>
                >>[/color]
                >
                >[/color]


                Comment

                Working...