|
-
Apr 27th, 2004, 11:10 PM
#1
Thread Starter
Lively Member
When is ~<classname> called actually ?
In the below example when is the ~Worker() method called exactly ?
class ch03_11
{
static void Main()
{
Worker worker = new Worker();
worker.Dispose();
}
}
public class Worker: System.IDisposable
{
private bool alreadyDisposed = false;
public Worker()
{
System.Console.WriteLine("In the constructor.");
}
public void Dispose(bool explicitCall)
{
if(!this.alreadyDisposed)
{
if(explicitCall)
{
System.Console.WriteLine("Not in the destructor, " +
"so cleaning up other objects.");
// Not in the destructor, so we can reference other objects.
//OtherObject1.Dispose();
//OtherObject2.Dispose();
}
// Perform standard cleanup here...
System.Console.WriteLine("Cleaning up.");
}
alreadyDisposed = true;
}
public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}
~Worker()
{
System.Console.WriteLine("In the destructor now.");
Dispose(false);
}
}
-
Apr 28th, 2004, 07:56 PM
#2
Frenzied Member
When the object is out of scope and is about the get destroyed.
-
Apr 28th, 2004, 11:13 PM
#3
Thread Starter
Lively Member
exactly thats what i have read, but...
i have put a breakpoint on it...
And it is called in neither of the cases
when i explicitly dispose() the object
or when i just skip the disposing.
-
Apr 29th, 2004, 09:41 AM
#4
It is supposed to be called directly before the garbage collector frees the associated memory. It's therefore quite unpredictable.
Try to add an explicit call to the garbage collector somewhere when you know that such an object is supposed to get destroyed and see if you get the breakpoint then. If not then it probably never breaks into finalizers (that's the .Net term, though C# uses the name destructor I believe).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 4th, 2004, 11:51 AM
#5
yay gay
I thought you couldn't manually call the Garbage Collector?
\m/  \m/
-
May 4th, 2004, 11:57 AM
#6
PowerPoster
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|