Solved

object lifespan

Posted on 2006-10-26
7
358 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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