Question about WeakReferences
<p>I've got a question regarding WeakReferences.</p>

<p>I'm right now in the process of writing a "Resource Manager" who hast to keep references to created texture objects. I have a Dictionary like:</p>

<pre><code>Dictionary&lt;uint, WeakReference&gt;
</code></pre>

<p>Where the first is, as you allready may guessed, the Resource Id and the second param is a WeakReference to the Resource itself.</p>

<p>Right now my Resources do have a method to free themselfes from their Owner (i.e. Resource Manager). They do so in calling a method at the Resource Manger while passing a this reference to it. The ResMgr does lookup if it is a resource he keeps bookmark of and if so, does something like this:</p>

<pre><code>WeakReference result;
if (m_Resources.TryGetValue(ResourceId, out result))
{
  if (result.IsAlive)
   return;

  (result.Target as Resource).free(); // free for good

  m_Resources.Remove(ResourceId);
}
</code></pre>

<p>The Problem I'm having is that the part after:</p>

<pre><code>if (result.IsAlive)
</code></pre>

<p>is never reached because there are still leftover references to the Resource.</p>

<p>The thing is, I do only have one Reference of the Resource in question and it releases itself like:</p>

<pre><code>resource.free(); // calls internally its owner (i.e. ResMgr)
resource = null;
</code></pre>

<p>I guess the left over reference would be the "resource" variable, but I cannot set it to null, because I do have to call free first. Quite a dilema...</p>

<p>Well what I wanted to achive with this is a Resource Manager who keeps references to its owning Resources and release them ONLY if there is no reference left to not screw up something.</p>

<p>Any idea how I may solve this in a clean fashion?</p>

<p>PS: Is there a way to actually get the reference count in some way ?</p>

<p>Thanks in advance!</p>

<p>Mfg Imp </p>
