473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function returning an object: memory issues?

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
2 2483

"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
Thank you, Anthony, for your very clear answer.

Ivor
Jan 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alfonso Morra | last post by:
Hi, What is the recomended way of returning an STL container (e.g. std::string, std::vector etc fom a function? Is it by simply returning a local variable? (I doubt it) std::string...
6
by: Generic Usenet Account | last post by:
Is it okay to return a local datastructure (something of type struct) from a function, as long as it does not have any pointer fields? I think it is a bad idea, but one of my colleagues does not...
1
by: Guha | last post by:
I have a problem with returning a 2D array using a function which is called in main(). The piece of the code is given below. This is a test code only. #include"stdio.h" #include"alloc.h" ...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
6
by: Jack White | last post by:
Does anyone know if an analogue to the "swap()" function found in C++ exists in C#. For those not familiar, let's say you have a function that loads some collection passed in by reference. If an...
14
by: Nickolay Ponomarev | last post by:
Hi, Why does http://www.jibbering.com/faq/ uses new Function constructor instead of function expressions (function(...) { ... }) when defining new functions? E.g. for LTrim and toFixed. Is the...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.