Dispose on nested Disposable items?

后端 未结 3 465
礼貌的吻别
礼貌的吻别 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题