HowTo find out the number of references to a reference type?

  • Thread starter Thread starter Klaus Drechsel
  • Start date Start date
K

Klaus Drechsel

My Problem:
The Function Dispose on a IDisposable shuld be called on the last reference
to this instance. Can I get the number of references. to an object?

Klaus Drechsel
 
The Function Dispose on a IDisposable shuld be called on the last
reference
to this instance. Can I get the number of references. to an object?

You cannot find out the number of references to an object.
However, the garbage collector will keep track of it and will call the
destructor/finalizer of objects that are no longer referenced.
 
A workaround might be to use a private static integer variable
that you increase in the class constructor.
public Constructur ( )
{
_instances ++;
}
private int _instances = 0x0;

Assuming all instances call Dispose, then you could just do:
public void Dispose ( )
{
if (0x1 >= (-- _instances))
{
// Do disposing
}
}

Or place that snippet in the finalizer and manually call Dispose from the
finalizer.
 
Hi Dennis,
seems, this count the number of instances totaly. I ned the number of
references to an instance, constructed once.
 
Ok, the dtor/finalizer is called by garbage collector.
I want to free ressources (close a file or a data connection) on time of
last usage.
Seems, I must find an other way,
Thanks
 
Ok, the dtor/finalizer is called by garbage collector.
I want to free ressources (close a file or a data connection) on time of
last usage.
Seems, I must find an other way,

Why another way? dtors/finalizer are just for that!
 
Yes that is true, but a solution to that could be to place
if (0x1 >= (-- _instances))
{
Dispose();
}
in the destructor, and maybe even make the Dispose method private and not
implement IDisposable
and instead do this critiacal cleanup in the finalizer.
 
In this case you have a problem: the RAII pattern/determinisic destructor
calls is impossible in .NET.
You have to invent reference couinting for your class. You have to
encapsulate your native resource class in another class. Each time you add a
reference you have to increment the counter. For removal of a reference you
have to manually call dispose.
 
Ho Cody,
the time of calling dtor/finalizer by GC is not defined. I must free the
ressource immediatelyand want to use the interface IDisposable (like "using
(v) {}").
 
Klaus Drechsel said:
My Problem:
The Function Dispose on a IDisposable shuld be called on the last reference
to this instance. Can I get the number of references. to an object?

Not from .NET. You'll have to implement reference counting manually,
including having client code explicitly notify you when it's done with the
object, and call Dispose() yourself when the count goes down to 0.
 
Thanks for the link
Klaus
Scott Allen said:
Klaus:

Just wanted to give you some additional food for thought:

Finalizers in managed code are not nearly as straightforward as they
might sound. There are threading and performance issues to consider,
as well as some best practice guidlines to follow.

See "Finalize and Dispose Explained" in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/
scalenetchapt05.asp
for more information.
 
Back
Top