473,324 Members | 2,257 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,324 software developers and data experts.

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

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
Nov 16 '05 #1
12 2036
> 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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #2
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.
"Klaus Drechsel" <kl************@prosytec.com> wrote in message
news:Op****************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #3
Hi Dennis,
seems, this count the number of instances totaly. I ned the number of
references to an instance, constructed once.

"Dennis Myrén" <de****@oslokb.no> schrieb im Newsbeitrag
news:NY*****************@news2.e.nsc.no...
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.
"Klaus Drechsel" <kl************@prosytec.com> wrote in message
news:Op****************@TK2MSFTNGP09.phx.gbl...
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


Nov 16 '05 #4
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

"cody" <no****************@gmx.net> schrieb im Newsbeitrag
news:eD**************@tk2msftngp13.phx.gbl...
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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

Nov 16 '05 #5
This won't work, because all referencing objects would have to call dispose
manually.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Dennis Myrén" <de****@oslokb.no> schrieb im Newsbeitrag
news:NY*****************@news2.e.nsc.no...
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.
"Klaus Drechsel" <kl************@prosytec.com> wrote in message
news:Op****************@TK2MSFTNGP09.phx.gbl...
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


Nov 16 '05 #6
> 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!

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #7
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.
"cody" <no****************@gmx.net> wrote in message
news:e8**************@TK2MSFTNGP12.phx.gbl...
This won't work, because all referencing objects would have to call dispose manually.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Dennis Myrén" <de****@oslokb.no> schrieb im Newsbeitrag
news:NY*****************@news2.e.nsc.no...
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.
"Klaus Drechsel" <kl************@prosytec.com> wrote in message
news:Op****************@TK2MSFTNGP09.phx.gbl...
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



Nov 16 '05 #8
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) {}").

"cody" <no****************@gmx.net> schrieb im Newsbeitrag
news:#w**************@TK2MSFTNGP09.phx.gbl...
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!

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

Nov 16 '05 #9
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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
"Klaus Drechsel" <kl************@prosytec.com> schrieb im Newsbeitrag
news:Oe**************@TK2MSFTNGP09.phx.gbl...
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) {}").

"cody" <no****************@gmx.net> schrieb im Newsbeitrag
news:#w**************@TK2MSFTNGP09.phx.gbl...
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!

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk


Nov 16 '05 #10

"Klaus Drechsel" <kl************@prosytec.com> wrote in message
news:Op****************@TK2MSFTNGP09.phx.gbl...
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.
Nov 16 '05 #11
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/de...netchapt05.asp
for more information.

--
Scott
http://www.OdeToCode.com
On Thu, 1 Jul 2004 11:56:42 +0200, "Klaus Drechsel"
<kl************@prosytec.com> wrote:
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


Nov 16 '05 #12
Thanks for the link
Klaus
"Scott Allen" <bitmask@[nospam].fred.net> schrieb im Newsbeitrag
news:g8********************************@4ax.com...
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/de...us/dnpag/html/
scalenetchapt05.asp for more information.

--
Scott
http://www.OdeToCode.com
On Thu, 1 Jul 2004 11:56:42 +0200, "Klaus Drechsel"
<kl************@prosytec.com> wrote:
My Problem:
The Function Dispose on a IDisposable shuld be called on the last referenceto this instance. Can I get the number of references. to an object?

Klaus Drechsel

Nov 16 '05 #13

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

Similar topics

4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
15
by: tom | last post by:
Hi, How do I get the serial number of the harddisk in .NET? I want this to be the same number even if the user has reformatted, so I do not want the volume serial number. Thanx, t
3
by: ATS | last post by:
HOWTO Implement LoadLibrary, GetProcAdress, and FreeLibrary. Below is code that I want to be able to use simple LoadLibrary\GetProcAddress\FreeLibrary technqiues on. I've used the code that was...
1
by: Olav Tollefsen | last post by:
When using Web Services from ASP.NET 1.1, you got a "Web References" folder in your project containing an entry for the web service references you added. The property page for this entry contained...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
8
by: Topper | last post by:
Hello. I have simple web folders structure: -ROOT - BIN WebService.dll WebService.asmx I need to use my WebService.dll not in bin folder - for example, in ROOT. How do i this? How can i do...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
22
by: mehdi_mousavi | last post by:
Hi folks, Consider the following line of code: Address addr = ei.Address; where ei is an instance of a class that has got a property of Address type. The question is that what is happening...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.