should I and how to do free resources?

Cheetah

Well-known member
Joined
Oct 12, 2006
Messages
232
Programming Experience
Beginner
OT: When I have finished with a new object of class I have created, should I and how to do free resources used by it?

Thanks.
 
Depends on what's used, COM objects usually need to be manually released, almost everything else is released when it falls out of scope

In my classes I usually do include a Close() method, which is used to ensure all DB connections are closed when you're done with the class
 
Or you can implement IDisposable which is the preferred method to "close" a class. I use this method on any class which accesses data in any way. If it simply holds parameters or variables, no need for this. But if it reads a text file or has any resource access outside of itself, it is a good idea to implement this interface and free resources the class uses.
 
What about somewthing like:

VB.NET:
Dim frm as New Form
Form.ShowDialog
Form.Dispose

Is that how I would release all resources used by the form? Because when I look in Task Managed the mem usage doesn't go down?
 
Dispose on a form level would release all the resources used by the form itself. It would not/should not release resources that *you* coded into routines on that form.

If the form eventually falls out of scope, you won't notice immediate results anyway because things are put out for garbage collection. And that only happens every so often.

Also keep in mind that Dispose does not destroy the object whatsoever; the object lives on until it falls out of scope, just as it would even if you hadn't Disposed of it. "Dispose" is purely a method for organizing your cleanup efforts and is good-practice in more complex classes.

The only way you can really (and unsafely for that matter) pseudo-destroy an object is to set it to nothing. And even then, it technically isn't destroyed; it is just left out for garbage collection. There is no way in .NET to destroy an object in the truest sense of the programming jargon. This is what makes .NET easy to code with, but it has an incredibly complex back-end to take care of all the mess that is left behind.

As one example, Dispose is useful if you have a loop in a class which accesses an object which you happen to set to nothing and then dispose of the class (for instance, a long operation that you may want the user to be able to cancel). That loop can check to see if "Disposed" is true, and exit softly before accessing the now-null object rather than throwing an "Object reference not set to the instance of an object" exception.

See http://www.vbdotnetheaven.com/Uploa...tNet04202005062448AM/DestructorsVbDotNet.aspx for a pretty decent explanation.

Bottom line is: if you're not using API/COM (Imported API calls, ActiveX, OCX controls etc) you technically never have to Dispose or free any resources (though it's a good idea to clean up to help the Garbage Collector out). They will be freed automatically (and that's the only way it can truly be done).
 
Last edited:
Will add that link to my reading list - thanks.

What do you mean by 'falls out of scope'?

Could you expand on this a little please?

Thanks.
 
\/ he explains it much better. :) My head hurts...

Basic premise of what I was trying to say: "falls out of scope" means a variable which nothing else refers to any longer.
 
Last edited:
"Falls out of scope" means when there are finally no more references to the object/no longer in the stack, i.e. no more objects have access to, or control over, it. As a for-instance, after a simple form - with a button which does nothing - closes completely, the form, along with all the controls on that form, are no longer "in scope".

Now, say you had a routine assigned to that button's Click event on that form... In the Click event handler, you declare a new class, then run a routine from that class which just contained an endless loop with a DoEvents inside it... That class now is in-scope along with the form that called it.

You can close the form, however the class and the form are both still in scope, because the class' routine has not passed control back to the form. So thus the form and class are still in-scope, even though the form has disappeared. If you somehow exit that loop (maybe it counted to a million), the form will then be allowed to fall out of scope (and close completely). Now, as long as you didn't access that same class (meaning the instance of it, not just the class itself) you created anywhere else, it, too, will fall out of scope along with the form.

Hope this helps; this was about the least complex explanation I could think of. :)
Scope seems to be something that many people intuitively understand but cannot explain properly. Objects and classes are never in scope or out of scope. Scope refers to variables only. A variable is in scope when the block in which it was declared is executing. As soon as that block completes it is then out of scope.

Now reference type variables can refer to objects on the heap. The object is never in scope or out of scope. If you declare a variable inside a method then that variable is in scope from the point that it's declared until the end of the method. It may, at different times, refer to various different objects. That's no matter. The variable is always in scope and the object is neither in or out of scope.

Now comes the good bit. An object on the heap is flagged as available for garbage collection when there are no more variables in scope that refer to it. That's the connection between scope and objects.
 
Ah Gotcha!

I understand the scope of variables now - it's something I knew, but didn't know what it was. Thanks for explaining.

As for "flagging as available for garbage collection", just so i can confirm my understanding.

If I had the following code

VB.NET:
Public insClass as myClass = New myClass

Private Sub OpenForm()

   'Declare the instance of the form.
   Dim newForm As New frmView

   'Assign it to a property of a class
   insClass.AttachedForm = newForm

   'Show the form.
   newForm.Show

End Sub ' newForm variable now out of scope.



Public Class frmView

   Public Sub Form_Closing(...) Handles Me.Closing

      insClass.AttachedForm = Nothing

   End Sub

End Class

Public Class myClass

   Private _attachedForm as frmView

   Public Property AttachedForm() as frmView
      Get
         Return _attachedForm
      End Get
      Set(ByVal value as frmView)
         _attachedForm = value
      End Set
   End Property

End Class

...the new instance of of the form would be flagged for garbage on closing, because no variable has it referenced?

I may not have used the exact syntax/spelling as i wrote this by hand.

Thanks.
 
As long as nothing else is referring to that class instance then yes your form will be ready for the GC.
 
Back
Top