Solved

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

Posted on 2001-06-16
7
151 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now