In vb6 we have terminate events. Code that must be called when object is destroyed.
Do we have that on vb.net?
Printable View
In vb6 we have terminate events. Code that must be called when object is destroyed.
Do we have that on vb.net?
you could use the Form_Disposed event:
vb Code:
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed '... End Sub
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:
Events are usually a much better option. What are you trying to do here?Code:Protected Overrides Sub Finalize()
Try
'Clean up your unmanaged resources here.
Finally
'Base cleanup
MyBase.Finalize()
End Try
End Sub
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.
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.
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.
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?
Not usually, no. But your problem (from your other thread) most likely does not have anything to do with memory leaks.