To Dispose() or Not To Dispose() elements in an array of IDisposable objects?

Denis

There are lots of examples of Arrays or Lists of IDisposable objects being returned from functions in .NET. For example, Process.GetProcesses().

  • If I call that method is it my responsibility to Dispose() of all the members of the array as I iterate through them?
  • Why should it be my responsibility since I never created the objects and the array that I was given is just pointers to the objects which were created outside of my code.

I always thought it was the creator's burden to Dispose(). So what is the proper rule here?

Servy

There is no general rule. It's going to depend on the situation, and how the method in question is designed, as to whether or not "you" are responsible for disposing of objects you have access to. This is where documentation is often important to help users of the type understand their responsibilities.

I always thought it was the creator's burden to Dispose()

This cannot be strictly true. It is sometimes the case that a disposable object will out-live the lifetime of the block of code creating it. While it simplest when the creator can dispose of the object, sometimes it's simply impossible for them to be able to. When returning a disposable object from a method is one situation where it's often not be possible for the code creating the disposable object to clean it up, as it's lifetime needs to be smaller than the lifetime of the disposable object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does NUnit dispose of objects that implement IDisposable?

From Dev

Dispose will dispose objects that are still referenced?

From Dev

Why is IDisposable instantly calling Dispose()?

From Dev

Dispose of objects in user control?

From Dev

How to Dispose SharpDX objects

From Dev

Font objects dispose in InitializeComponent

From Dev

Would MVC call Dispose if your ViewModel was IDisposable?

From Dev

Does disposing an object dispose of all IDisposable properties?

From Dev

When/how is IDisposable.Dispose called?

From Dev

Why code analyzers recommend to dispose IDisposable fields?

From Dev

Where to call Dispose() of IDisposable created in constructor?

From Dev

Reasoning behind implementing Dispose but not implement IDisposable?

From Dev

Would MVC call Dispose if your ViewModel was IDisposable?

From Dev

C++ dispose array

From Dev

How to properly dispose objects in Flambe?

From Dev

Must implement Sub Dispose() for interface 'System.IDisposable'

From Dev

IDisposable.Dispose is never called after exception in using block

From Dev

Do i need to Dispose Stream when i Pass it to IDisposable class?

From Dev

If I have a Dispose method, must I implement IDisposable?

From Dev

Using Unity, how do you automatically dispose an IDisposable?

From Dev

What should I do when inheriting IDisposable with nothing to dispose?

From Dev

How does WebResponse not have "Dispose" publically visible when it implements IDisposable?

From Dev

C# calling IDisposable.Dispose() vs making object null

From Dev

How can i dispose IDisposable when it is declared in a parameter?

From Dev

IDisposable.Dispose() not called in Release mode for async method

From Dev

Using Unity, how do you automatically dispose an IDisposable?

From Dev

Must implement Sub Dispose() for interface 'System.IDisposable'

From Dev

IDisposable.Dispose is never called after exception in using block

From Dev

Warning: Do not dispose objects multiple times

Related Related

  1. 1

    Does NUnit dispose of objects that implement IDisposable?

  2. 2

    Dispose will dispose objects that are still referenced?

  3. 3

    Why is IDisposable instantly calling Dispose()?

  4. 4

    Dispose of objects in user control?

  5. 5

    How to Dispose SharpDX objects

  6. 6

    Font objects dispose in InitializeComponent

  7. 7

    Would MVC call Dispose if your ViewModel was IDisposable?

  8. 8

    Does disposing an object dispose of all IDisposable properties?

  9. 9

    When/how is IDisposable.Dispose called?

  10. 10

    Why code analyzers recommend to dispose IDisposable fields?

  11. 11

    Where to call Dispose() of IDisposable created in constructor?

  12. 12

    Reasoning behind implementing Dispose but not implement IDisposable?

  13. 13

    Would MVC call Dispose if your ViewModel was IDisposable?

  14. 14

    C++ dispose array

  15. 15

    How to properly dispose objects in Flambe?

  16. 16

    Must implement Sub Dispose() for interface 'System.IDisposable'

  17. 17

    IDisposable.Dispose is never called after exception in using block

  18. 18

    Do i need to Dispose Stream when i Pass it to IDisposable class?

  19. 19

    If I have a Dispose method, must I implement IDisposable?

  20. 20

    Using Unity, how do you automatically dispose an IDisposable?

  21. 21

    What should I do when inheriting IDisposable with nothing to dispose?

  22. 22

    How does WebResponse not have "Dispose" publically visible when it implements IDisposable?

  23. 23

    C# calling IDisposable.Dispose() vs making object null

  24. 24

    How can i dispose IDisposable when it is declared in a parameter?

  25. 25

    IDisposable.Dispose() not called in Release mode for async method

  26. 26

    Using Unity, how do you automatically dispose an IDisposable?

  27. 27

    Must implement Sub Dispose() for interface 'System.IDisposable'

  28. 28

    IDisposable.Dispose is never called after exception in using block

  29. 29

    Warning: Do not dispose objects multiple times

HotTag

Archive