Results 1 to 9 of 9

Thread: Destructor /Terminate

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Destructor /Terminate

    In vb6 we have terminate events. Code that must be called when object is destroyed.

    Do we have that on vb.net?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Destructor /Terminate

    you could use the Form_Disposed event:

    vb Code:
    1. Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
    2. '...
    3. End Sub

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Destructor /Terminate

    The most direct equivalent is Finalize() I suppose (because it will be called no matter what when the object is destroyed), but never use it without Dispose() (and you won't even need Dispose() much either) unless you have a good reason:
    Code:
    Protected Overrides Sub Finalize()
         Try
             'Clean up your unmanaged resources here.
         Finally
             'Base cleanup
             MyBase.Finalize()
         End Try
    End Sub
    Events are usually a much better option. What are you trying to do here?
    Last edited by minitech; Mar 22nd, 2011 at 12:58 PM.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Destructor /Terminate

    It depends on the object. Some objects raise the Disposed event (those almost always inherit the Component class or implement the IDisposable interface somehow) and most don't.
    If an object does raise the Disposed event then you can handle that event (as in Paul's example for a Form). If an object doesn't have that event - that's most likely because it's not needed - you can always subclass it and raise your own Disposed event.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Destructor /Terminate

    It's a complicated and difficult topic in .NET.

    .NET's memory is automatically cleaned up by an entity called the Garbage Collector (GC). For the most part, you don't know when the GC runs and you don't care. If an object needs to release resources in a hurry, it has to implement that mechanism itself.

    There's one hook for the GC. If an object implements a special method called a finalizer, the GC executes it as part of cleanup. You can implement a finalizer on a class in VB .NET by declaring a Sub Finalize(), but there's several reasons why a user might not want to implement a finalizer.

    There is a pattern called the Dispose pattern that .NET follows that doesn't have the disadvantages of finalizers. Unfortunately it's really difficult to get it right and it's a convention, not something built in to .NET. Here's an MSDN example, but you really have to read several to get a feel for it. I'll skip implementation detals since you didn't ask how to implement it.

    A class that implements the pattern may choose to raise an event when it is disposed, but it's not required; it's not even part of most tutorials on the pattern.

    So the answer in general is no, .NET has no guaranteed mechanism for getting a notification when an object is removed from memory. But there are mechanisms objects may implement to know when they are being destroyed. If they implement these mechanisms, they may provide a way for you to know. But it's not universal.

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Destructor /Terminate

    If you have an object that needs disposing, inherit from the IDisposable interface. When you press the <enter> key after typing 'Inherits IDisposable' it will add the necessary methods to your form automatically.

    You will generally need to do this if your object creates, for example, objects which themselves need disposing, for example, Graphics.Pen object.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Destructor /Terminate

    Quote Originally Posted by SJWhiteley View Post
    If you have an object that needs disposing, inherit from the IDisposable interface. When you press the <enter> key after typing 'Inherits IDisposable' it will add the necessary methods to your form automatically.

    You will generally need to do this if your object creates, for example, objects which themselves need disposing, for example, Graphics.Pen object.
    It should be "Implements" for an interface
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Destructor /Terminate

    I see. The reason I asked is webclient objects tend to have problems after I run the program for 10 minutes. I wonder if there is some form of memory leak.

    Memory leak is something that won't happen on vb.net right?

  9. #9
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Destructor /Terminate

    Not usually, no. But your problem (from your other thread) most likely does not have anything to do with memory leaks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width