none
Object not finalizing

    Question

  • I stumbled on this problem. C# in Visual Studio 2015.

    public Class A { private Data localdata; public A() { } public void GetResources() { // Gets resources for localdata. } public void ReleaseResources() { // Releases resources. } public void UseResources() { // Use the resources. } public bool UsingResources() { // true if have resources to use; // false otherwise. return <if using resources>; } } public class B { private bool isdisposed = false; public B(ref A objectA) { A.GetResources(); } ~B() { Dispose(false); } public Adata { get { // Certain access to A.localdata } } public void Dispose() { Dispose(true);

    GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (isdisposed) return; isdisposed = true; if (disposing) { FinalizeManaged(); } FinalizeUnmanaged(); } private void FinalizeManaged() { // Clean up B stuff } private void FinalizeUnmanaged() { // Release A resources if (Adata.UsingResources) { Adata.ReleaseResources(); } } public void ProcessData() { Adata.UseResources(); } }


    I run the program, I create A, I create B. B processes the data then leaves scope. I didn't call B.Dispose() thinking that it would be called anyway when leaving scope. But it wasn't called. Never.

    void MyMethod()
    {
        MyAye = new A();
        MyBee = new B(ref MyAye);
        MyBee.ProcessData();
        return;
    }
    
    

    I run MyMethod(). I used the Debugger and verified that ~B() was never called.

    When I explicitly added MyBee.Dispose(), I verified that ~B() was called.

    In C++, an object is automatically finalized when leaving scope. (If I forget to call C++Object.Destructor(), I don't have to worry.)

    so what happened?


    VP

    Saturday, June 04, 2016 2:14 AM

Answers

  • I do not think that destructor (finalizer) is called automatically when leaving the scope: “The programmer has no control over when the destructor is called because this is determined by the garbage collector” [https://msdn.microsoft.com/en-us/library/66x5fx1b.aspx].

    If you need a deterministic behaviour, then implement and call a function like Close, or implement Dispose and use the using statement.

    Saturday, June 04, 2016 9:05 AM

All replies

  • I do not think that destructor (finalizer) is called automatically when leaving the scope: “The programmer has no control over when the destructor is called because this is determined by the garbage collector” [https://msdn.microsoft.com/en-us/library/66x5fx1b.aspx].

    If you need a deterministic behaviour, then implement and call a function like Close, or implement Dispose and use the using statement.

    Saturday, June 04, 2016 9:05 AM
  • I do not think that destructor (finalizer) is called automatically when leaving the scope: “The programmer has no control over when the destructor is called because this is determined by the garbage collector” [https://msdn.microsoft.com/en-us/library/66x5fx1b.aspx].

    If you need a deterministic behaviour, then implement and call a function like Close, or implement Dispose and use the using statement.


    OK, I had forgotten about this when it was late and my thinking wasn't so clear...

    VP

    Saturday, June 04, 2016 5:12 PM
  • I do not think that destructor (finalizer) is called automatically when leaving the scope:

    ...

    If you need a deterministic behaviour, then implement and call a function like Close, or implement Dispose and use the using statement.

    OK, now I remember the issue!!!

    This becomes an important issue if the program decides to take a CORE dump for any reason beyond my ability to call .Dispose().

    So if I can't ~Finalize() an object that makes C++ more important than C# .... or is there really a way, how?


    VP

    Wednesday, June 15, 2016 2:30 PM
  • Hi VP,

    Since this thread has been closed, I suggest you create a new thread to describe your problem.

    And this forum is discuss Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor. According to the code in your original post, I think your problem should be related to C# code. Please post your problem to C# forum for a better help. Thanks for your understanding.

    Best Regards,
    Weiwei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 16, 2016 1:03 AM
    Moderator