Results 1 to 6 of 6

Thread: When is ~<classname> called actually ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    110

    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);
    }
    }
    Anis Bombaywala

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    When the object is out of scope and is about the get destroyed.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2003
    Posts
    110

    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.
    Anis Bombaywala

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  5. #5
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    I thought you couldn't manually call the Garbage Collector?
    \m/\m/

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You can most certainly manage the Garbage Collector:
    http://msdn.microsoft.com/library/de...collection.asp

    More specific to collection:
    http://msdn.microsoft.com/library/de...collection.asp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width