Dispose on nested Disposable items?

后端 未结 3 458
礼貌的吻别
礼貌的吻别 2021-01-18 21:21

I wanted to know if there are any conventions regarding disposal of disposable items nested inside another disposable item(in a property/public field, not as private members

相关标签:
3条回答
  • 2021-01-18 22:05

    The best approach is often to supply with the nested IDisposable items an indicator of whether the nested item should be disposed with the container, unless the useful life of the item will never exceed that of the container (in which case disposing the container can dispose the item), or one can be sure that the last entity other than the container to need the contained item will be aware of its existence and need for disposal (meaning the container doesn't have to worry about disposal, since the other item can handle it).

    As a simple example, suppose one were designing a UI framework and wanted to provide a control which is supposed to display an Picture, and provides a means by which code can supply an image to be displayed. Assume further that some types of Picture have resources that need to be disposed. There are some situations in which code might want to display a certain Picture on multiple controls, which might not all be disposed at the same time. In such a situation, it would be bad if disposing a control were to dispose its picture. On the other hand, there are other situations where code might create a picture for the purpose of being displayed, give it to a control, and then not care about it any more. In such a situation, the code supplying the picture might know that the picture should be disposed when the control no longer needs it, but might not know when that would occur.

    Using a parameter to indicate whether a picture should be Disposed would allow for clean code in both of the above scenarios. An alternative approach, which is what Winforms uses, is to have events which occur when either the control's picture changes or when the Image property is changed. Code which sets the Image property of a control to an image which needs disposal can use those events to take care of it.

    0 讨论(0)
  • 2021-01-18 22:09

    The rule of thumb I normally follow is that the class that creates a disposable object, will also dispose it. As an example: a SqlCommand does not dispose its connection, because it didn't create it. The StreamReader has a strange behavior in this sense, because it will always dispose the underlying stream, even if it is supplied from the outside (I find this very annoying, please vote HERE when you like Microsoft to fix this).

    0 讨论(0)
  • 2021-01-18 22:12

    I would say that usually a container will dispose any contained disposable items - StreamReader disposes of the underlying stream, for example - but typically I will dispose of each item with a separate using statement anyway.

    Any concept of "ownership" is really just in documentation and convention. Basically you have to know what will dispose of what, which typically means reading the documentation and hoping it makes it clear. Unfortunately it doesn't always do so :(

    Note that there's no single correct answer here - sometimes you may want a type to behave one way, and sometimes the other. Some types explicitly allow you to state whether you're effectively transferring ownership of the resource, although most don't.

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