Results 1 to 13 of 13

Thread: Set somthing = Nothing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question Set somthing = Nothing

    Example:
    VB Code:
    1. Private Sub Display()
    2.              Dim rs As New ADODB.Recordset
    3.              .
    4.              .
    5.              .
    6.              .
    7.              'Set rs = Nothing  
    8.    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?

  2. #2
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    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
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question Re: Set somthing = Nothing

    Quote 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.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. Dim rs As New ADODB.Recordset
    You should use this:
    VB Code:
    1. Dim rs As ADODB.Recordset
    2.              Set rs = New ADODB.Recordset

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Question Re: Set somthing = Nothing

    Quote 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:
    1. Dim rs As New ADODB.Recordset
    You should use this:
    VB Code:
    1. Dim rs As ADODB.Recordset
    2.              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:
    1. Dim rs As ADODB.Recordset
    2.              Set rs = New ADODB.Recordset
    [/QUOTE]

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Set somthing = Nothing

    Yep, when you Dim 'as New ...' like this:
    VB Code:
    1. 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.

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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
  •  



Click Here to Expand Forum to Full Width