|
-
Nov 4th, 2005, 10:03 PM
#1
Thread Starter
Addicted Member
Set somthing = Nothing
Example:
VB Code:
Private Sub Display()
Dim rs As New ADODB.Recordset
.
.
.
.
'Set rs = Nothing
End Sub
Some said that rs will be set to Nothing automatically by Vb, but some said that it won't. How it was? How can i know whether it is set to Nothing automatically? Is there any example to test it?
-
Nov 4th, 2005, 10:16 PM
#2
Fanatic Member
Re: Set somthing = Nothing
i think calling up some value in the db with rs(without Dim rs As New ADODB.Recordset)
should reveal that.
but i'm not sure if vb automatically set rs to nothing
-
Nov 4th, 2005, 10:16 PM
#3
Re: Set somthing = Nothing
You have to set it to Nothing. Some objects (Like WORD) will actually stay in memory even after your app ends if you don't set them to Nothing.
-
Nov 4th, 2005, 10:37 PM
#4
Thread Starter
Addicted Member
Re: Set somthing = Nothing
 Originally Posted by dglienna
You have to set it to Nothing. Some objects (Like WORD) will actually stay in memory even after your app ends if you don't set them to Nothing.
So does it mean that some object will be set to Nothing automatically, but some will not? We should set it Nothing every time.
-
Nov 4th, 2005, 11:37 PM
#5
Re: Set somthing = Nothing
I think objects are unloaded when the app closes (according to joacim) but not all of them. It's a good habit to get into if you always set all objects to Nothing. That way you don't have to worry about them, and your app will always end when you unload forms.
-
Nov 4th, 2005, 11:46 PM
#6
Re: Set somthing = Nothing
An object will not be set to nothing if it has other dependant objects holding a reference to it still. Also, some COM objects may still be instanciated after your program closes unless you close and destroy all their references.
I noticed that your using a recordset for an example. You should always .Close your rs and then set it to nothing.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 5th, 2005, 10:07 AM
#7
Re: Set somthing = Nothing
I completely agree with RobDog888.
I would also like to mention an issue with the code posted - when you Dim as "New" anything, it slows down the code which uses this object, as each time you reference it VB has to check whether or not it has already been initialised (and set it if not).
Instead of this:
VB Code:
Dim rs As New ADODB.Recordset
You should use this:
VB Code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
-
Nov 6th, 2005, 11:07 PM
#8
Thread Starter
Addicted Member
Re: Set somthing = Nothing
 Originally Posted by si_the_geek
I completely agree with RobDog888.
I would also like to mention an issue with the code posted - when you Dim as "New" anything, it slows down the code which uses this object, as each time you reference it VB has to check whether or not it has already been initialised (and set it if not).
Instead of this:
VB Code:
Dim rs As New ADODB.Recordset
You should use this:
VB Code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
I am interested in what si the geek said. I don't understand cleary.
Can you tell me the different between the following clearly(the process of the program)?
vbcode]
Dim rs As New ADODB.Recordset
[/Highlight]
And
VB Code:
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
[/QUOTE]
-
Nov 6th, 2005, 11:45 PM
#9
Re: Set somthing = Nothing
Yep, when you Dim 'as New ...' like this:
VB Code:
Dim rs As New ADODB.Recordset
..VB does a bit of 'clever' memory management, rather than create all objects at the point of definition (the Dim line), it creates them when they are first used (ie: the first line of code which uses rs).
In terms of the memory used this is very good, as you do not have lots of objects in memory which aren't being used - if you have lots of these in the General Declarations section this would be as soon as your form loads (or when the program loads if they are in modules!).
Unfortunately, in order to do this the object has to be created when it is first used - and to do this you need to check every time that it is used if it has already been created, and this checking process takes time. For objects like recordsets (which are often used hundreds of times) the slow-down effect of all of these checks is significant.
-
Nov 7th, 2005, 08:34 AM
#10
Re: Set somthing = Nothing
Maybe a habit from other programming languages but I always set an object to nothing after finishing with it to ensure there are not memory leaks from the program.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Nov 22nd, 2005, 08:47 AM
#11
Re: Set somthing = Nothing
Actually an object is never set to Nothing. What you do is to set the object reference to Nothing, and by doing so release the memory used by that reference. In your case you use a local variable as the reference and it will fall out of scoop when the procedure ends so setting it to Nothing is not really necassary (even though I think it's a good practice to do so). VB will destroy the reference when the procedure ends which will decrease the reference count in the actual object and if it then has reached zero (no other references) the object is destroyed.
Dglienna mentioned that, for example, a Word.Application object will not be destroyed and that is true, however your reference is deleted and the reference counter is indeed decreased. But since Word is loaded out-of-process it will remain in memory unless you explicitly close the application. However that is not done by setting the reference to Nothing, it is done by calling the Quit method. So it's important to close references like database connections and recordsets but you don't close those either by setting your reference to Nothing, you do it by calling the Close method.
When an application ends VB will destroy all object references but it will not close connections or call any other methods of the object to remove out-of-process objects from memory, that is your resposibility.
So, in VB you don't have to set a reference to Nothing if you use it correctly, but it might free up some memory if you do. On the other hand if you use VBScript in a classic ASP application it is more important to set object references to Nothing since IIS will not destroy a created object if you don't. There is a difference between creating an object using Server.CreateObject compared to the regular CreateObject function. With this said I think it's best to always remember to set an object reference to Nothing so if you switch development environment you don't forget to do it when it's necassary to do so.
Also, as you use a loose bound object (you use the New keyword on the same line as the Dim statement) it really doesn't matter if you set the reference to Nothing since if you by accident would use a property or call a method with that reference variable after doing so the object is simply recreated. So always use strong bound objects as ychhuong and RobDog mentioned above.
-
Nov 22nd, 2005, 02:06 PM
#12
Re: Set somthing = Nothing
Umm, that was Si, Joacim. But I have said it before too. 
Just giving credit where the credit is due.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 22nd, 2005, 08:17 PM
#13
Re: Set somthing = Nothing
Oh yeah, sorry... You both use that ugly color on your user names
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|