Function returning an object: memory issues?

I

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
 
A

Anthony Jones

Ivor Somerset said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top