IDisposable, does it really matter

后端 未结 9 3000
予麋鹿
予麋鹿 2021-02-20 09:19

Coming from C/C++ a long time ago I still have a habit of ensuring that all resources are cleaned up correctly. I always ensure Dispose is called on IDisposable classes and impl

相关标签:
9条回答
  • 2021-02-20 09:48

    Yes, Yes, Yes, It matters.

    I've been profiling an application recently that had never been profiled. It's just a Winforms application, no big deal, right?

    Wrong.

    By not implementing IDisposible and not de-referencing event handlers, the application was leaking memory like a sieve.

    The .NET Framework does not absolve you from cleaning up after yourself, it just makes it less likely that you'll break something if you don't.

    Spend an hour, profile your application with the ANTS Profiler. It's a free trial. If you don't see any memory leaks, then continue on your way. If you do, then it's because you were relying on the .NET Framework to be your crutch.

    0 讨论(0)
  • 2021-02-20 09:49

    I had a case which I unfortunately cannot remember the details of, but it was some kind of layered streams. The lowerlevel file stream was sometimes closed before the upperlevel text formatter was flushed, which caused the last output written to the text formatter to be lost.

    0 讨论(0)
  • 2021-02-20 09:58

    Not disposing (or closing) database connections will eventually bite you, yeah. I've seen that happen.

    0 讨论(0)
提交回复
热议问题