IDisposable, does it really matter

后端 未结 9 2999
予麋鹿
予麋鹿 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:47

    Yes, I also ran into an issue with Connection objects to an Oracle database not getting disposed.

    Mike Atlas's issue above is bad, but it was at least clear about what was going wrong. The issue we ran into was that from time to time under heavy load, the site would start throwing errors when we tried to open a connection, but by the time we looked at the system it had all cleared up (because the garabe collector had cleared the objects and freed up the connection pool). It was very difficult to reproduce, until I was looking through the code and noticed that a connection was not being closed in the event of an error, changing this to a using statment fixed the whole issue.

    The short answer is that if an object takes the effort to implement IDisposable, it's there for a reason, so ALWAYS dispose it when you are done, ideally with a using statement. Don't get clever or tricky with disposing sometimes but not other times when you don't think you need to blah blah blah. Just do what works every time.

    The shorter, and more satisfying answer, is that you are right, and your coworkers are morons who don't know what they are doing.

提交回复
热议问题