none
My feedback about disposing RRS feed

  • General discussion

  • I want that compiler when compile program check if all diposable object was disposed and do not compile program if it is not or show warning.

    This is my feedback to Microsoft. I don't know where to write my idea.

    Thursday, September 29, 2016 3:26 PM

All replies

  • Not entirely sure this is completely possible in the most general sense without hitting some kind of "halting problem"? 

    On the other hand, you can already get analyzers that do look for non-disposed disposable objects in the most common situations, warn you and even refactor for you (e.g. Code Cracker).

    Thursday, September 29, 2016 3:37 PM
  • That isn't really possible 100% of the time since you cannot account for exceptions. However static code analysis to ensure you are (most likely) cleaning up your IDisposable objects  is already supported via Code Analysis. Turn on CA for your projects (and optionally tell it to fail builds) and you'll get what you want.

    Rule 1

    Rule 2

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, September 29, 2016 3:45 PM
    Moderator
  • I want that compiler when compile program check if all diposable object was disposed and do not compile program if it is not or show warning.

    This is my feedback to Microsoft. I don't know where to write my idea.

    What you want is a language like C++ where the lifetime of all objects is deterministic then.  Enforcing disposal of objects (at compile time) in C# isn't possible in the general case due to the halting problem.

    I do wish Microsoft had taken the approach with disposable objects provided by the .NET framework that if the finalizer of the object runs and the object hasn't been disposed that some notification to the programmer would be given.  The main problem with that is that IDisposable is pretty viral.  Add an IDisposable object as a class member somewhere and you might have to refactor your entire program to be IDisposable -- and then you wind up with a language that is like C++ with no destructors.


    • Edited by SimonRev Thursday, September 29, 2016 4:04 PM
    Thursday, September 29, 2016 4:03 PM
  • Like preventing race conditions, not forgetting to dispose of stuff is in the end your job. If a computer could make this check for you reliably, they would have done so already.
    You are basically asking for a compiler that is one step closer to not needing either of us and I would prefer to not be useless quite yet, thank you :)

    Also there might be some missunderstanding about the Finalize/Dispose pattern:
    First, everything that is finalizeable will be finalized by the GC when it next runs. That will be latest at application closure. That the GC never forgets to free a resource is one big advantage it has. That you never know whe it does so, is the natural downside.

    With disposeable stuff, there are 3 cases:

    1. Everything directly handling unmanaged resources (like Filehandles, Networkconnection) should first implement a Finalizer so that at least the GC can clean up at the end. Then you should implement IDisposeable as convenience for the using programmer.

    2. Everything only handling something that Implement IDisposeable (but not directly handling unmanaged stuff itself) should implement only IDisposeable, for the sole purpose of relaying Dispose to whatever it contains.

    3. Ideally however, you should never even keep a Disposeable instance around in the first place.
    Create it. Use it. Dispose of it. All in the same piece of code, ideally using a using-block.
    Keeping the reference around tends to bite you in the butt, so do not keep it around in the first place.

    Prefer case 3.
    Avoid case 2.
    And if you ever have to make a case 1, then it is a wierd day indeed. All the likely 1 Cases are covered by .NET Framework classes already.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Thursday, September 29, 2016 5:59 PM