Click here to Skip to main content
15,174,547 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Why do we set a recordset object to nothing after retrieving? What is the technical idea behind that?

eg.
VB
dim  rsTemp

set rsTemp = Server.CreateObject("ADODB.recordset")
' after that I retrieve some data
rsTemp.Open "select * from Rates" blah, blah

then I see something like this:
VB
set rsTemp = nothing

Why should we set it to nothing?
Posted
Updated 9-Dec-10 3:47am
v2

The usual reason for this is that the RecordSet takes up resources on the computer - memory, possibly clock cycles etc. so it is a good idea to dispose of it as soon as it has served its purpose so that it can be garbage collected and its resources freed up.

This applies to any object that your code creates. If the object implements IDisposable, you should also call its Dispose method.

Without seeing the context that your code example comes from it is not possible to tell if the assignment to nothing is necessary, but it is always a good idea to do it as soon as possible, rather than waiting for the framework to do it for you.
   
Comments
fjdiewornncalwe 9-Dec-10 9:23am
   
Spot on.
Bassofa 9-Dec-10 9:25am
   
Thanks for your answer.
The reason for that is because those objects weren't always destroyed like they should in past times. COM objects are normally automatically released when they get out of scope and have no references set to them. For some COM objects this was somewhat problematic in the past. For some it was necessary and for others (or other environments) it wasn't. But to simply make sure that it does, those lines where added to set them to nothing specifically. The recordset would need to be closed as well by the way. Also, if the connection to the database is still open and that object still has a reference to the opened recordset, then it is of course very likely that it isn't released until the connection is closed.

As what I know it shouldn't really matter for ASP because after the request is handled, everything (except maybe session data) is released.

Good luck!
   
Comments
fjdiewornncalwe 9-Dec-10 9:23am
   
Same comment as Henry's... Spot on.
Bassofa 9-Dec-10 9:25am
   
Thanks for your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900