Function returning an object: memory issues?
am 08.01.2007 10:33:40 von Ivor Somerset
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
Re: Function returning an object: memory issues?
am 08.01.2007 11:11:04 von Anthony Jones
"Ivor Somerset" wrote in message
news:45a20fe8$0$300$426a74cc@news.free.fr...
> 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