Question

protected override void Finalize()
        {
            try
            {
                this.FtcpSock.Close();
                this.FudpSock6800.Close();
                this.FudpSock6801.Close();
                this.FudpSock6802.Close();
                this.FudpSock6803.Close();
                this.FudpSock6804.Close();
                this.FudpSock6806.Close();
            }
            finally
            {
                this.Finalize();
            }
        }

I m getting this Error Message:

Error 1 Do not override object.Finalize. Instead, provide a destructor.

by the way, this is Original Code complied by third party company.
how to solve this problem? how to Finalize with override?

Was it helpful?

Solution 2

If you can edit the code, you can move the code to a destructor. For a form called 'Form1' the destructor would look like:

~Form1()
{
    this.FtcpSock.Close();
    this.FudpSock6800.Close();
    this.FudpSock6801.Close();
    this.FudpSock6802.Close();
    this.FudpSock6803.Close();
    this.FudpSock6804.Close();
    this.FudpSock6806.Close();
}

http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

OTHER TIPS

As the message states, you shouldn't override Finalize directly but instead provide a destructor. Ignoring that though the code has one other fatal problem

protected override void Finalize() {
  try { 
    ...
  } finally { 
    this.Finalize();
  }
}

This code will eventually lead to a stack overflow in the code. It is recursively calling itself in a finally block and hence will just loop until you run out of stack space. The code was likely meant to use the following instead

base.Finalize();

This will chain to the implementation of Finalize on the base type

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top