Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2001-06-16
7
Medium Priority
?
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

722 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