المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Use of GC.SuppressFinalize in ref counted objects



C# Programming
07-15-2009, 07:20 PM
Afternoon all,

I'm looking for advice or opinions on the best placement of GC.SuppressFinalize for reference counted objects.

The "standard" dispose pattern has the call in the public Dispose method :-

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}


However, for objects with reference counts I'm inclined to put it in the protected Dispose :-

protected void Dispose(bool _fulldispose)
{
if (!m_Disposed)
{
if (RemoveRef() == 0)
{
if (_fulldispose)
{
GC.SuppressFinalize(this);

etc...


My reasoning is that if not all references are explicitly disposed, then the finaliser will still be needed to do the clean up. (Although in our system all objects with unmanaged components should be explicitly disposed, so that may be academic.)

There are lots of discussions about Dispose, IDisposable, SuppressFinalize etc. out on the net, but very few deal with the ref counted situation, so I'm open to any thoughts or advice...

There are three kinds of people in the world - those who can count and those who can't...