References to object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do i determine if there are any references to an object

The object is a dataset that very rarely changes. It is loaded at app start up and used as the data source for combos etc
On the rare occasion it changes i want to refresh it, but only if no references exist

Im refreshing it, but what is happening is if a combo box in on display that uses it it stuffs up

So i only want to refresh it if no references exist

The GC does not seem to have any method like GetReferenceCount(object

Regard
Michael
 
The GC does not seem to have any method like GetReferenceCount(object)

That's because .NET doesn't use reference counters like COM did
(deterministic finalization). .NET uses non-deterministic finalization
which means that we really don't know when an object will be released from
the heap.

What you might try is setting some "flag" of your own in the DataSet's
Dispose() method, since this is the method that will get called when the
DataSet falls out of scope (which would happen when no other objects are
using it).

If you could do what you wanted, how would that work? If no objects were
referencing the DataSet, it would be removed from memory. If it was removed
from memory, how would you refresh it?
 
Scot

Thanks for that

I wasnt very clear. Of course the application will always have a reference to it - it creates it. Other parts of the app get a reference to it when needed, eg, a form is displayed. When the form closes its reference is dropped. So now i can refresh it

I realise that this is exactly what the old com reference counting was, and even though its good to get rid of the pesky thing, in this instance i need something similar

I was hoping the GC would expose this. It must do something similar internally.
 
Are you talking about having the DataSet declared as global throughout your
assembly? Because, once you close the form (if that is indeed where you
created the DataSet), you can't refresh it. It will drop out of scope at
that point.

As I mentioned, if you want to monitor the DataSet to see when nothing is
referencing it any longer, set your own flag in the DataSet's dispose
method, which fires when the object falls out of scope.

If you know that you want the DataSet refreshed when the form closes, just
re-generate it in the form's close event.


Michael said:
Scott

Thanks for that.

I wasnt very clear. Of course the application will always have a reference
to it - it creates it. Other parts of the app get a reference to it when
needed, eg, a form is displayed. When the form closes its reference is
dropped. So now i can refresh it.
I realise that this is exactly what the old com reference counting was,
and even though its good to get rid of the pesky thing, in this instance i
need something similar.
I was hoping the GC would expose this. It must do something similar
internally.
 
Michael said:
How do i determine if there are any references to an object.

The object is a dataset that very rarely changes. It is loaded at app
start up and used as the data source for combos etc.
On the rare occasion it changes i want to refresh it, but only if no references exist.

Im refreshing it, but what is happening is if a combo box in on display that uses it it stuffs up.

So i only want to refresh it if no references exist.

The GC does not seem to have any method like GetReferenceCount(object)

Regards
Michael

What do you mean by "if a combo box in on display that uses it it stuffs
up"? What exactly is the problem you are experiencing? The reason I ask is
that I have tried a quick sample app with a dataset used "globally", bound
it to a combo box in a form, then refreshed the dataset from a different
form. Everything worked fine (no exceptions were thrown, and the combobox
displayed the new information).

Perhaps you could explain how you are making the dataset available and how
you are handling the databinding to your controls. Maybe someone could spot
a change that would keep the app from "stuffing up".

As for reference counting, the GC does not use it. There are several
articles on various websites (like msdn.microsoft.com) that explains how the
GC works.
 
Michael,

GC.GetReferenceCount(object) would be a complete non-sense, for two reasons:

1) The GC does not use reference counting (or maybe not *only* reference
counting, a sophisticated GC may mix ref counting with other algos) because
reference counting is too weak to detect cycles of dead objects.

2) Even if the GC used reference counting, the object that you pass as
argument must be "referenced" at least once at the time you pass it (even it
this is just by a local variable), So, the call would always return a count
= 1. Useless for what you want to do.

On the other hand, you can create a WeakReference to your object. This will
allow you to retrieve the object as long as it is referenced through at
least one strong reference, and when the object will be GCed, your
WeakReference will return null. Seems like this is what you want.

Bruno.

Michael said:
How do i determine if there are any references to an object.

The object is a dataset that very rarely changes. It is loaded at app
start up and used as the data source for combos etc.
 
Back
Top