Solved

object lifespan

Posted on 2006-10-26
7
362 Views
Last Modified: 2010-04-16
Hi have a question about an objects lifespan.

I have a static method in a class that returns an object to a null object.

MyClass myclass;

myclass = ClassServices.GetClass(1);

What happens to the object that got created in the GetClass method? Does it get destoyed after being assigned to 'myclass'?




0
Comment
Question by:tmueller18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17815728
No. Your method will return a reference to the object which will be stored in the variable myclass. The object will not be destroyed until sometime after the last reference to it goes out of scope. So in your case it won't be destroyed until at least the time that myclass goes out of scope. Unless of course you are returning a clone of the object rather than a simple reference in which case the original cloned object will drop out of scope at the end of the GetClass method (assuming it isn't a static object and isn't referenced from anywhere else).
0
 

Author Comment

by:tmueller18
ID: 17815776
the GetClass static method likes like this

public static PersonClass GetClass(int ID)
{
      PersonClass result= new  PersonClass();
      return result;
}

---------------------------
calling page:
----------------------------
PersonClass myPersonClass;
myPersonClass = PersonServices.GetClass(3);


what happens to 'result'?

From what you said the result object doesnt live outside the scope of the method. Is this correct?

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17815817
"result" isn't actually an object, it is a variable that holds a reference to an object.

"result" will drop out of scope at the end of the method, but the object you are returning will not because a reference to the object is still being held by myPersonClass outside of the GetClass method. The actual object being referenced will not drop out of scope until at least the time that the "myPersonClass" variable drops out of scope.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tmueller18
ID: 17815846
"result" isn't actually an object, it is a variable that holds a reference to an object.

'result'  is an object since it was instantiated.

'result' is passed to the variable 'myPersonClass'

result drops out of scope now?



0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17815886
"result" isn't an object, "result" simply points to an object.

Think of your object as a balloon; "result" is a piece of string through which you can get to the balloon, when you return from your method you are tying another piece of string (the "myPersonClass" variable) to the balloon. When the method exits the "result" piece of string is cut off and destroyed, the balloon stays exactly where it was and is still being referenced by the "myPersonClass" variable.

So, the "result" variable drops out of scope when the method exits, but the actual object you created still remains in memory and is now referenced by the "myPersonClass" variable.
0
 

Author Comment

by:tmueller18
ID: 17815971
so when i create an object like this:

Foo f = new Foo();

f isnt an object but a pointer to an object thats created in memory?

i understand the baloon part. thanks for explaing the scope of the variable

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17818109
Yes, basically.

f is a variable that holds a reference to an object of type Foo. then the "new Foo()" part creates an object in memory somewhere and stores the address for that block of memory in the variable f.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 46
Need a simple RegEx to search for two words 27 88
C# Linq Select From List 3 49
C# Get Current Exchange Rate from XE 9 18
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question