472,354 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 2439

"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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.