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

Reference Counting

MP
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 "unreferenced"
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.
Nov 16 '05 #1
7 6473
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:
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 "unreferenced"
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.

Nov 16 '05 #2
MP
Thank you Alex, I will.

-MP
"Alex" <www.flashshop.info> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
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:
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
"unreferenced" 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.

Nov 16 '05 #3
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_You_My_Address> wrote in message
news:OD*************@TK2MSFTNGP14.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 have a
little problem to resolve. Our old COM cache would cleanup "unreferenced"
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.

Nov 16 '05 #4
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.GetIUnknownForObject(comObject);
int count = Marshal.Release(intptr);


"Sean Hederman" wrote:
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_You_My_Address> wrote in message
news:OD*************@TK2MSFTNGP14.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 have a
little problem to resolve. Our old COM cache would cleanup "unreferenced"
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.


Nov 16 '05 #5
MP
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" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
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_You_My_Address> wrote in message
news:OD*************@TK2MSFTNGP14.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 have
a little problem to resolve. Our old COM cache would cleanup
"unreferenced" 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.


Nov 16 '05 #6
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_You_My_Address> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...
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" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
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_You_My_Address> wrote in message
news:OD*************@TK2MSFTNGP14.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 have a little problem to resolve. Our old COM cache would cleanup
"unreferenced" 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.



Nov 16 '05 #7
MP
Thank you Wayne. I'll have a look at it.

Have a nice day!
"Wayne" <Me******@community.nospam> wrote in message
news:ef****************@TK2MSFTNGP09.phx.gbl...
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_You_My_Address> wrote in message
news:uz**************@TK2MSFTNGP14.phx.gbl...
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" <us***@blogentry.com> wrote in message
news:cu**********@ctb-nnrp2.saix.net...
> 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_You_My_Address> wrote in message
> news:OD*************@TK2MSFTNGP14.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 have >> a little problem to resolve. Our old COM cache would cleanup
>> "unreferenced" 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.
>>
>
>



Nov 16 '05 #8

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

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
1
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last...
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
0
by: Kalle Rutanen | last post by:
Hello I implemented reference counting in my program, and found out many problems associated with it. I wonder if the following problems can be solved automatically rather manually ? 1. ...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.