0

As a programmer, I wonder since GC (Although we don't know when it starts to work, but according to MSDN, it will release resources that aren't referred by any active threads...ect). Since these resouces are USELESS, why don't we put codes of managed resource releasing in the destructor function? Something like this following:

public class A
{
           private bool _isDisposed = false;
        
            public void Dispose()
            {
              if(!_isDisposed){
                  // Release unmanaged and managed things
                   ……
                  // Set to false to mark this class has been totally freed...
                  _isDisposed = true;
              }
           }
            ~A()
            {
               Dispose();
            }
}

Any special reasons why we ONLY PUT unmanaged things in the destructor function but NOT ALL?

Take this as an example:

A a = new A();
a = null;

Something but very simple to tell you that the instance "a" WON'T BE REFERRED by any active threads (or else it cannot be collected by GC). So why don't let the destructor function release all the resources in it???

Beginner
  • 15
  • 3
  • Your question, as is, is somewhat pointless since the code as shown doesn't even _need_ a destructor / finalizer / idisposable. So in the context of that _specific_ code my suggestion would be - remove them all. – mjwills Jul 10 '21 at 07:47
  • 2
    Also, in 15+ years of C# development I have *never* written a finalizer. It is _incredibly rare_ to write one nowadays. So even though I could answer the question for you, it is somewhat of a moot point - since in practice the information is useless. There is no real point learning the mechanisms of a thing that in practice you never need to do. – mjwills Jul 10 '21 at 07:48
  • @mjwills:Sorry but to save time, this is just my simple code. From MSDN I see that the GC will start to work when a class has a destructor and release unmanaged things out. But the question for me now is no matter when it starts to work, it always collect useless things, so here comes my wondering question: why don't we release them all? – Beginner Jul 10 '21 at 07:52
  • `Sorry but to save time, this is just my simple code.` Your simple code doesn't save time - it confuses the issue. But if you really, want the nitty gritty details - start with https://web.archive.org/web/20160420151055/https://blogs.msdn.microsoft.com/cbrumme/2004/02/20/finalization/ . Particularly the `Reachability` heading onwards. – mjwills Jul 10 '21 at 07:52
  • @mjwills: I've never written something like this, either. But just feel curious about it, for Microsoft's webpage has something about it. – Beginner Jul 10 '21 at 07:53
  • 1
    Trust me, save yourself some time. Ignore it. You will never write a finalizer in your entire career. Understand the _principle_ (which is you shouldn't touch another managed resource from a finalizer, which the blog post I linked to explains why) - but realise it doesn't matter since you will never write a finalizer anyway. – mjwills Jul 10 '21 at 07:54
  • To answer your direct question - why you cannot. The answer is either / both of "because there is no need to" and "because it might break". – mjwills Jul 10 '21 at 07:56
  • Also, your destructor should _never_ call `IDisposable.Dispose` (like you are in your code, well, if you implemented `IDisposable`). – mjwills Jul 10 '21 at 08:01
  • Maybe https://stackoverflow.com/questions/2605412/why-should-we-call-suppressfinalize-when-we-dont-have-a-destructor?rq=1 answers some of your questions. – PMF Jul 10 '21 at 08:53

0 Answers0