Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP
Why do we set a recordset object to nothing after retrieving? What is the technical idea behind that?
 
eg.
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:
set rsTemp = nothing
Why should we set it to nothing?
Posted 9-Dec-10 2:43am
Bassofa737
Edited 9-Dec-10 2:47am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Marcus Kramer at 9-Dec-10 9:23am
   
Spot on.
Bassofa at 9-Dec-10 9:25am
   
Thanks for your answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
  Permalink  
Comments
Marcus Kramer at 9-Dec-10 9:23am
   
Same comment as Henry's... Spot on.
Bassofa at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 686
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100