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

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
 
C

cody

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.
 
D

Dennis Myrén

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.
 
K

Klaus Drechsel

Hi Dennis,
seems, this count the number of instances totaly. I ned the number of
references to an instance, constructed once.
 
K

Klaus Drechsel

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
 
C

cody

This won't work, because all referencing objects would have to call dispose
manually.
 
C

cody

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!
 
D

Dennis Myrén

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.
 
C

cody

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.
 
K

Klaus Drechsel

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) {}").
 
M

Mike Schilling

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.
 
K

Klaus Drechsel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top