By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

Function returning an object: memory issues?

P: n/a
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
Jan 8 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Ivor Somerset" <so*******@hiddenembassy.comwrote in message
news:45*********************@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
Jan 8 '07 #2

P: n/a
Thank you, Anthony, for your very clear answer.

Ivor
Jan 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.