Function returning an object: memory issues?

Discussion in 'ASP General' started by Ivor Somerset, Jan 8, 2007.

  1. Hello,

    In my ASP code I sometimes write functions that return an object
    (generally an XML node).

    Such a function is invoked this way:
    Set Object1 = MyFunction(SomeValue)

    And at the end of the process the object will be properly destroyed:
    Set Object1 = Nothing

    Now, in the function body, an object is instantiated that bears the name
    of the function:

    Function MyFunction(SomeArgument)
    Set MyFunction = (...)
    End Function

    My question is: what about memory, given that the object instantiated in
    the function *cannot be set to nothing* (no object would ever be
    returned to the main process)? Does ASP have some garbage collector that
    deals with this situation?

    Thanks in advance for your answer.

    Ivor
    Ivor Somerset, Jan 8, 2007
    #1
    1. Advertising

  2. "Ivor Somerset" <> wrote in message
    news:45a20fe8$0$300$...
    > Hello,
    >
    > In my ASP code I sometimes write functions that return an object
    > (generally an XML node).
    >
    > Such a function is invoked this way:
    > Set Object1 = MyFunction(SomeValue)
    >
    > And at the end of the process the object will be properly destroyed:
    > Set Object1 = Nothing
    >
    > Now, in the function body, an object is instantiated that bears the name
    > of the function:
    >
    > Function MyFunction(SomeArgument)
    > Set MyFunction = (...)
    > End Function
    >
    > My question is: what about memory, given that the object instantiated in
    > the function *cannot be set to nothing* (no object would ever be
    > returned to the main process)? Does ASP have some garbage collector that
    > deals with this situation?
    >
    > Thanks in advance for your answer.
    >
    > Ivor


    A key concept that you need to understand is the difference between an
    object instance and an object reference. Variables hold object references
    not the object instance itself. Hence :-

    Dim o1, o2

    Set o1 = New MyClass
    Set o1 = o2
    ' At this point there is only 1 instance of a MyClass object but there are
    two references
    Set o1 = Nothing
    ' At this point one of the references has been released but the instance
    will still exist since there
    ' is still an outstanding reference
    Set o2 = Nothing
    ' Now that the outstanding reference is has been released the instances
    reference count has reached 0. At this point the object destroys itself
    releasing any memory it has allocated.

    Now look at this:-

    Function MyFunc()
    Set o = new MyClass
    ' do stuff to o
    Set MyFunc = o
    End Function

    Set mo = MyFunc()

    Just before the end of MyFunc there are two references to an instance of
    MyClass.
    Just after the MyFunc completes and it's return value has been assigned to
    mo only mo has a reference to the MyClass instance that was created in
    MyFunc. When the variable o in MyFunc passes out of scope at the end of the
    function it's content is automatically set to nothing for you. The
    reference in the 'MyFunc' varaible is copied to the mo variable (no new
    reference is created).

    At the end of the script mo passes out of scope an VBScript automatically
    sets it to nothing which causes the object to destroy itself and release
    memory.

    I tend to eliminate the temporary o variable in such a function and just
    use:-

    Function MyFunc()
    Set MyFunc = New MyClass
    ' Do stuff to MyFunc
    End Function


    Anthony
    Anthony Jones, Jan 8, 2007
    #2
    1. Advertising

  3. Thank you, Anthony, for your very clear answer.

    Ivor
    Ivor Somerset, Jan 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pablo J Royo
    Replies:
    7
    Views:
    424
    Jerry Coffin
    Sep 23, 2003
  2. Christopher J. Bottaro

    Returning from socket.accept and threading issues.

    Christopher J. Bottaro, Oct 19, 2004, in forum: Python
    Replies:
    3
    Views:
    394
    Elbert Lev
    Oct 20, 2004
  3. Replies:
    11
    Views:
    662
    Christos Georgiou
    May 2, 2006
  4. Jason
    Replies:
    10
    Views:
    642
    Jason
    Aug 29, 2007
  5. Jean-Michel Pichavant

    Mocked object returning another Mocked object

    Jean-Michel Pichavant, Jul 13, 2012, in forum: Python
    Replies:
    1
    Views:
    199
    Ulrich Eckhardt
    Jul 13, 2012
Loading...

Share This Page