0

I have spent a lot of time researching the disposing of objects, the disposable interface and finalizers. I have mostly understood how it works, but I was not able to find a clear stance on what to do with some types of fields once an object has been disposed.

To illustrate, I have a class that stores some object references in fields:

public class ImageCropper : IDisposable
{
    private Image workImage;
    private Point cropOrigin;
    private List<MoveIncrement> moveAmounts;
    private Movie movie;
}

My Dispose method then does the following (omitting the handling of the disposed state here in the example):

protected virtual void Dispose(bool disposing)
{
    workImage.Dispose(); // Is disposable, so as a general rule, dispose of it
    // cropOrigin is not disposable, ignore it
    // moveAmounts is not disposable, ignore it
    movie = null; // Not disposable, but remove the reference to help the garbage collector
}

Is this the right way to do it?

AeonOfTime
  • 956
  • 7
  • 10
  • 2
    Does this answer your question? [Implementing IDisposable correctly](https://stackoverflow.com/questions/18336856/implementing-idisposable-correctly) – Heretic Monkey May 13 '20 at 12:59
  • 5
    `// Not disposable, but remove the reference to help the garbage collector` -- that does nothing useful, don't bother – canton7 May 13 '20 at 12:59
  • Setting the field to `null` would only help if the code tried to access that field *after* it the object was already disposed, which clearly a bug. That's why BCL classes actually set and check a `_disposed` flag and throw an exception. – Panagiotis Kanavos May 13 '20 at 13:02
  • 1
    InBetween makes a *very* important observation - `disposing` is there for a reason, so you *don't* try to use objects when called by the finalizer. At that point, all other objects in those fields may already be garbage-collected. An `Image` isn't gd'd automatically so it needs to be disposed. `moveAmounts` on the other hand could already be gc'd – Panagiotis Kanavos May 13 '20 at 13:04
  • @PanagiotisKanavos I deleted the comment because I missed that the OP explicitly says "omitting the handling of the disposed state here in the example"... I'm guessing he is aware of the significance of `disposing`. I he's not, your comment should warn him he's missing something. – InBetween May 13 '20 at 13:27
  • @HereticMonkey: It answers it in part only - it does not go into detail on which types of fields have to be cleared / disposed. The question there was mostly because he had not implemented the disposable interface correctly. – AeonOfTime May 13 '20 at 13:29
  • @PanagiotisKanavos: I omitted the handling of the diposed state in the example, as I wanted to focus on the fields and their comments. Maybe that's too confusing - should I add that back in? – AeonOfTime May 13 '20 at 13:31
  • 1
    There are a number of answers on that question, including [this one](https://stackoverflow.com/a/18336981/215552) and [this one](https://stackoverflow.com/a/18337072/215552), which mention that managed, non-Disposable fields do not need to be handled in `Dispose(bool disposing)` – Heretic Monkey May 13 '20 at 15:25
  • @HereticMonkey: Thanks. I would argue that those answers help if one has already understood the difference between managed and unmanaged resources. In my case, it was not yet clear, wich is why I added fields with different types in my question. What helped a lot more was [this answer](https://stackoverflow.com/a/31016954/2298192). – AeonOfTime May 13 '20 at 15:52

0 Answers0