Solved

Re: A Routine that closes all the object variables and set them to nothing

Posted on 2001-06-16
7
153 Views
Last Modified: 2010-05-02
I am looking for a function or routine that checks any open objects (recordsets, connections, controls etc) and closes them and set them to nothing. Can someone help in this regard...
0
Comment
Question by:smkkaleem
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6199983
Public Sub UnloadControls(Frm As Form)
Dim ctl As Control

  For Each ctl In Frm.Controls
    Set ctl = Nothing
  Next ctl
End Sub


And call it like this:
UnloadControls Me

hope this helps!
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6200108
JonFish85, that's not going to do anything useful!
The for loop will set ctl to each control, then you set ctl to nothing. The control is still there, you've just cleared the value of ctl.

VB's fairly tidy when it comes to closing down objects. You just have to be careful with data access because there's a few bugs in it. Closing and setting connections and recordsets to nothing immediately after you've used them is a good habit to get into.

You can do a quick loop to close all forms if you like - something like:

Dim frm As Form
for each frm in Forms
  unload frm
next

But it's nicer to be explicit about things.
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6200261
setting a control = nothing doesnt release it from memory? also, what if you sent (using sendmessage api), WM_DESTROY? Would that "kill" the control? I haven't done any work where I really had to worry about using too much memory with controls and such, so I dont know as much about it as I should...
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 43

Expert Comment

by:TimCottee
ID: 6201493
smkkaleem, There is no substitute for good practice in this case. As andyclap says it is a good idea to make sure that you close and release any recordset/connection objects immediately when you are finished with them. There is no easy way of finding out what these objects are once you have created them. It is really up to you to manage the lifetime of these obects in your code and ensure that you explicitly destroy all objects that you create. I know that generally speaking VB will release all such objects when the application shuts down. However this is not 100% guaranteed and you cannot verify this assumption easily anyway.
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6204450
JonFish85,
>setting a control = nothing doesnt release it from memory?
aha - but you're not setting the /control/ to nothing, you're setting the /variable/ ctl.

WM_DESTROY would probably get VB's knickers in a twist if it were sent to a controls window, you're best off just unloading the form and setting any references you have to it to nothing.

As mentioned before, VB's not too bad at releasing COM references (roll on proper garbage collection in .net though), however it's very easy to create a memory leak in VB through a circular reference: Object 1 holds a reference in a module level variable to Object 2, Object 2 holds a reference to Object 1 in the same way - neither COM object's refcount returns to 0, and both stay in memory - bye bye stability.


Incidentally, I also try to avoid using 'implicit' declarations, such as

dim x as new xxx

instead using

dim x as xxx
set x=new xxx

and when using forms I often treat them in the same way, using

dim f as form1
set f=new form1
f.show

rather than form1.show. I find this gives me more control over the lifecycle of objects.
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6204461
ah yes very true. If one were to go Set cmdButton = Nothing as opposed to the code I posted, would the button then be released from memory?
0
 
LVL 6

Accepted Solution

by:
andyclap earned 100 total points
ID: 6204493
Unfortunately the controls implicit references are read only, so you won't be able to compile this (it'll look for the control's default property and come up with an invalid use message).
Unloading the form will clean up the controls nicely, unless you've set another variable outside of the form to a control on the form which will keep the control alive. Remember the visual representation of the control on the form and the COM instance of the control's class are not the same thing - a control can happily exist without a window showing it. This is a bit of a simplification, but it's along the right lines.


0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change vba from autofit to 13.5 width? 4 29
Copy a row 12 61
Add and format columns in vb6 7 56
Using an encrypted  value to decrypt and display contents in vb6 9 51
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question