locked
form finalizer RRS feed

  • Question

  • How come the designer doesn't implement a finalizer for a form, but it implements Dispose()?  Shouldn't a form have a finalizer in case an application creates one, but forgets to call dispose when it is closed?  Or does closing a form automatically invoke Dispose()?
    Thursday, June 26, 2008 3:35 AM

Answers

  • Hi,

     

    Are you talking about the Finalize()? Yes you can implement Finalize() to clean up resources when the Dispose() method is not called, however keep it to clean only unmanaged resources (windows, files, and network connections,). You cannot override the Finalize() but the destructor automatically calls it up.

     

    A destructor looks like this:

    Code Snippet
    class MyClass
    {
        
    ~ MyClass()
    {
            // do the clean up
    }
     
    }

     

     

    The best practice is to implement a Dispose() or Close() and do the cleanup over there. However I would 

    advice you to take a look over here to understand this better.

     

    http://msdn.microsoft.com/en-us/library/498928w2(VS.80).aspx

     

    HTH,

    Suprotim Agarwal

    Thursday, June 26, 2008 5:15 AM
  • No, it has one.  It inherits the one from Component.  But that doesn't do much of anything relevant in the case of a Form.  The only thing that would normally require finalization is the Handle property. But it is the window handle that keeps a form instance alive in the first place.  If a form is not forcibly disposed, it dies a normal death when Windows sends it the WM_DESTROY message.  At which point the handle is no longer valid and doesn't need to be released anymore.
    Friday, June 27, 2008 4:51 AM

All replies

  • Hi,

     

    Are you talking about the Finalize()? Yes you can implement Finalize() to clean up resources when the Dispose() method is not called, however keep it to clean only unmanaged resources (windows, files, and network connections,). You cannot override the Finalize() but the destructor automatically calls it up.

     

    A destructor looks like this:

    Code Snippet
    class MyClass
    {
        
    ~ MyClass()
    {
            // do the clean up
    }
     
    }

     

     

    The best practice is to implement a Dispose() or Close() and do the cleanup over there. However I would 

    advice you to take a look over here to understand this better.

     

    http://msdn.microsoft.com/en-us/library/498928w2(VS.80).aspx

     

    HTH,

    Suprotim Agarwal

    Thursday, June 26, 2008 5:15 AM
  • No, it has one.  It inherits the one from Component.  But that doesn't do much of anything relevant in the case of a Form.  The only thing that would normally require finalization is the Handle property. But it is the window handle that keeps a form instance alive in the first place.  If a form is not forcibly disposed, it dies a normal death when Windows sends it the WM_DESTROY message.  At which point the handle is no longer valid and doesn't need to be released anymore.
    Friday, June 27, 2008 4:51 AM