Forum Discussion

Pakema's avatar
Pakema
Contributor
15 years ago

[VBScript] Shall I use "Set a = nothing"?

When program on vb we use "set a = nothing"



But in TC ?



f.e.



Set a = Aliaces.Sys.iexplore



in end of function shall I use "Set a = nothing" or not ?

3 Replies

  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 1 rankChampion Level 1
    Hi Nikita,



    According to good programming practices, yes, you should release reference to the object as soon as you no longer need it. However VBScript engine does this automatically when the variable that keeps the reference goes out of scope.
  • Alexei, thanks



    But I want know something else 



    Can anybody from AQA explain me about: How I can clean memory form object (f.e. set a = obj)(this is the main idea of this topic)? How work garbage collector in TC ? the same as in native VB or not?



    As I know form msdn http://msdn.microsoft.com/en-us/library/0x9tb07z.aspx (It's about VB)



    When you assign Nothing to an object variable, it no longer refers to any object instance. If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. The instance is terminated, and the memory and system resources associated with it are released, only after the garbage collector (GC) detects that there are no active references remaining.



    The next things http://msdn.microsoft.com/en-us/library/55yzhfb2(v=vs.90).aspx (about Class Destructor Methods)



    In Visual Basic 6.0, the Nothing keyword is used to disassociate an object variable from an actual object. The Set statement is used to assign Nothing to an object variable



    And end from wiki http://en.wikipedia.org/wiki/Destructor_(computer_science) 



    ...destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources (which includes closing database connections, releasing network resources, relinquishing resource locks, etc.)



    thanks
  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 1 rankChampion Level 1
    Hi Nikita,



    It is my understanding (that might be corrected by Support):

    -- TC does not implement any garbage collectors but uses the one provided by the script runtime engine (VBScript, JScript or DelphiScript);

    -- Setting the variable to Nothing decrements object's reference counter, but this is relevant for the COM objects only. That means, that if you start IE as a tested application and assigned a reference to the process to the a variable (Set a = Aliases.Sys.iexplore) and then execute the 'Set a = Nothing', this will not close the IE instance.