c# - Question about WeakReferences -


I have a question about weak references.

I'm in the process of writing a "resource manager" that has to make references to the texture objects created. I have a dictionary:

  dictionary & lt; Uint, WeakReference & gt;  

Where is the first, as you can guess all, there is a weak reference for the resource ID and the other ultimate resource.

Now my resources are a method for freeing them from their masters (i.e. resource manager). While passing this context, they do this by calling a method of resource manager. ResMgr If it is a resource, then searches for it and if so, it does something like this:

  WeakReference result; If (m_Resources.TryGetValue (Resource ID, out of results)) (return (result.IsAlive) return; (Result. Target as the resource.) Free (); // Good for m_Resources.Remove (resource IID); }  

The problem I have is that the part after that:

  if (result.islive)  
< P> Never reaches because there are still remaining references to the resource.

The thing is that I have a reference to the resource only in question and it will roll itself It does:

  resource.free (); // Internally its owner (i.e. resmgr) resource = null;  

me It seems that the left over context will be "resource" variable, but I can not empty it, because I have to make free calls for the first time very dilemma ...

Well I am aware with it Wanted to be a resource manager who keeps reference to their own resources and leaves them, if not screw them up There is no reference to the sector.

How any idea I could solve it in a clean fashion?

PS: Is there really any way to get reference count in some way?

Thanks in advance!

MFG

I recommend that you take a reference count or reference. Weak references were never used as caching systems.
There is no "reference number" in it The objects are either referenced or they are not. If you need to calculate the actual context, then I recommend to build on RefCountDisposable , which reference count IDisposable .


Comments