Do I need to implement a finalizer for a class using TcpClient?

I have a class (say MyClass) that uses (has as a private field) an object TcpClient. MyClassimplements a IDisposablecall TcpClient.Closein a method Dispose.

My question is: should a MyClasscall finalizer be implemented Dispose(bool Disposing)to free unmanaged resources TcpClient’sif the calling code is not called MyClass.Dispose?

thanks

+5
source share
4 answers

No, not worth it.

Since you should never call a method on another object in the finalizer , it could be modified to your object.

TcpClient , .

Dispose:

protected virtual void Dispose(bool disposing)
{
   if (disposing)
   { 
      // dispose managed resources (here your TcpClient)
   }

   // dispose your unmanaged resources 
   // handles etc using static interop methods.
}
+4

, .

:

. , ( ), , , GC , , . , , , .

/ , , :

/// <summary>
    /// Example of how to implement the dispose pattern.
    /// </summary>
    public class PerfectDisposableClass : IDisposable
    {
        /// <summary>
        /// Type constructor.
        /// </summary>
        public PerfectDisposableClass()
        {
            Console.WriteLine( "Constructing" );    
        }

        /// <summary>
        /// Dispose method, disposes resources and suppresses finalization.
        /// </summary>
        public void Dispose()
        {
            Dispose( true );
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes resources used by class.
        /// </summary>
        /// <param name="disposing">
        /// True if called from user code, false if called from finalizer.
        /// When true will also call dispose for any managed objects.
        /// </param>
        protected virtual void Dispose(bool disposing)
        {
            Console.WriteLine( "Dispose(bool disposing) called, disposing = {0}", disposing );

            if (disposing)
            {
                // Call dispose here for any managed objects (use lock if thread safety required), e.g.
                // 
                // if( myManagedObject != null )
                // {
                //     myManagedObject.Dispose();
                //     myManagedObject = null;
                //  }
            }
        }

        /// <summary>
        /// Called by the finalizer.  Note that if <see cref="Dispose()"/> has been called then finalization will 
        /// have been suspended and therefore never called.
        /// </summary>
        /// <remarks>
        /// This is a safety net to ensure that our resources (managed and unmanaged) are cleaned up after usage as
        /// we can guarantee that the finalizer will be called at some point providing <see cref="Dispose()"/> is
        /// not called.
        /// Adding a finalizer, however, IS EXPENSIVE.  So only add if using unmanaged resources (and even then try
        /// and avoid a finalizer by using <see cref="SafeHandle"/>).
        /// </remarks>
        ~PerfectDisposableClass()
        {
            Dispose(false);
        }
    }
+2

No, you do not need it. TcpClient is a wrapper class around an unmanaged socket, and there it is managed the way it should be removed. What you have done is enough.

+1
source

Yes you should - Microsoft even recommends it .

Just remember that the belt and suspender code never starts in the office at 2:00 in the morning :)

0
source

All Articles