본문 바로가기
Visual Basic .NET/Class

A destructor method in a class

by edupicker(체르니) 2008. 6. 26.

A destructor method in a class

Within a class, a destructor method lets the class perform “cleanup” processing before the garbage collector(GC) releases the memory that holds an object.
I explained GC through last lecture.
Let’s refer to last lecture http://fromyou.tistory.com/entry/memory-management-In-the-NET-environment

 

Well, To specify the processing you want a program to perform before an object is released, you must define the Finalize method.

When you use inheritance to derive one class from another, the garbage collector will first call the derived-class destructor method if the derived class defines the Finalize method. But If the derived class does not define the Finalize method, the garbage collector will instead call the base-class destructor.


Normally, within a destructor method, the last processing the code will perform is to call the base-class Finalize method, like
MyBase.Finalize()

 

I’ll explain the definition by using first case and second case.

At first, Within the GrandParents class, the Finalize method displays a message box stating “The Base Class Destructor is Running.”
Likewise, within the Parents class, the Finalize method displays a message box stating “The Derived Class Destructor is Running.”

I compiled and ran the program, the program will display a form that contains a FirstCase(Whose Text property is Firstcase)button1 you can use to create a FirstCase class. When you click the button, the code will create the class, causing the GrandParents class and the Parents class constructor methods to run. The destructor methods, however, will not run until you close the program. As you will recall, the garbage collector calls the destructor

method for an object that is no longer in use. In this case, the program does not discard the objects until it ends.


First case code


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim FirstCase As New Parents()

End Sub

 

'---related Button1_Click Event

Class GrandParents

    'This is GrandParents constructor

    Public Sub New()

        MsgBox("The GrandParents Class Constructor")

    End Sub

 

    'This is GrandParents destructor

    Protected Overrides Sub Finalize()

        MsgBox("The GrandParents Class Destructor")

    End Sub

End Class

 

Class Parents

    Inherits GrandParents

 

    'this is Parents constructor

    Public Sub New()

        MsgBox("The Parents Class Constructor")

    End Sub

 

    Protected Overrides Sub Finalize()

        MsgBox("The Parents Class Destructor")

        MyBase.Finalize()

    End Sub

End Class

'---related Button1_Click Event

 


And then Second Case creates three classes named First, Second, and Third.

Class Second inherits class First and class Third inherits class Second.
When I ran the program, my screen was first display

class Third’s destructor message, followed by class Second’s destructor message, and finally class First’s destructor message.

Second case code

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim SecondCase As New Third()

End Sub

'---related Button2_Click Event

Class First

    Public Sub New()

        MsgBox("In First class  constructor")

    End Sub

 

    Protected Overrides Sub Finalize()

        MsgBox("In First class  destructor")

    End Sub

End Class

 

Class Second

    Inherits First

 

    Public Sub New()

        MsgBox("In Second class  constructor")

    End Sub

    Protected Overrides Sub Finalize()

        MsgBox("In Second class  destructor")

        MyBase.Finalize()

    End Sub

 

End Class

 

Class Third

    Inherits Second

 

    Public Sub New()

        MsgBox("In Third class  constructor")

    End Sub

    Protected Overrides Sub Finalize()

        MsgBox("In  Third class destructor")

        MyBase.Finalize()

    End Sub

End Class

'---related Button2_Click Event

As you can see, within each class, the Finalize method calls the corresponding base-class destructor.