|
Don Asks:
When you create a variable in your ASP page and assign a string value of "hello" to it, should any clean up of this variable take place, or does this happen automatically when the ASP page is finished parsing?
David:
For all variables, this is true. BUT, for objects, if you do not SET them to Nothing, you will have memory leaks. Of course, if there is a .Open method, you should close it as well *before* setting it to nothing.
Tore:
If a recordset is not open or does not exist, attempting to close it will cause an error. In some cases (for example creating a recordset from a connection.Execute with no result rows returned) the recordset is not actually created.
However, there is nothing magic about a .Open method. It is just a method provided by the developer of an object. Usually, if the object was created by a programmer worth anything, an object that has an "Open" method should have a "Close" method (for explicit housekeeping). And the Class_Terminate event should check whether the user actually performed the Close, and if not, perform one implicitly. However, the more complex an object becomes, the more difficult this is to ensure. In some cases it may not even be possible. Other method pairs may exist for certain objects, and they may be called whatever the developer found meaningful at the time of implementation - Reserve/Release, Edit/Update[|Cancel], Start/Stop, etc.
It is an accepted "best practice" to always explicitly close and dereference objects. The following construct is safe for most ADO objects:
If Not adoObjSomething Is Nothing Then
If adoObjSomething.State = adStateOpen Then
adoObjSomething.Close
End If
Set adoObjSomething = Nothing
End If
This conversation string was taken from the 15Seconds ASP Listserv on 3/26/02. If you have an ASP-related question or would like to share some of your knowledge with others, you may join the list by clicking here.
|