Solved

object lifespan

Posted on 2006-10-26
7
354 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now