locked
How to dispose a class that doesn't implement IDisposable RRS feed

  • Question

  • User1094877758 posted

    The class Microsoft.SqlServer.Dts.Runtime.Application doesn't implement the IDisposable interface nor does it have a Close method.  What is the best practice to remove this object from the GC?

    On a high level, I was thinking the best practice would be to:

    1) Create a wrapper class of the Application class

    2) Implement the IDisposable interface

    3) In the Dispose method set the instance of the class to null.

    Please let me know if that's correct.

    Thanks!

    Tuesday, June 24, 2014 5:39 PM

Answers

  • User1779161005 posted

    Sounds like you're unclear on the difference between memory and non-memory resources. IDisposable is ultimately for non-memory/unmanaged resources. It has nothing to do with memory or the GC.

    The rule is if an object implements IDisposable then you need to call it because ot holds a non-memory resource that's not managed by the .NET runtime and needs to be cleaned up. Otherwise if it doesn't implement IDisposable then it doesn't have any resource other than memory (meaning the object itself) and in this case don't worry about it -- the GC will take care of it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 24, 2014 6:02 PM

All replies

  • User1779161005 posted

    Sounds like you're unclear on the difference between memory and non-memory resources. IDisposable is ultimately for non-memory/unmanaged resources. It has nothing to do with memory or the GC.

    The rule is if an object implements IDisposable then you need to call it because ot holds a non-memory resource that's not managed by the .NET runtime and needs to be cleaned up. Otherwise if it doesn't implement IDisposable then it doesn't have any resource other than memory (meaning the object itself) and in this case don't worry about it -- the GC will take care of it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 24, 2014 6:02 PM
  • User1094877758 posted

    Thanks for the explaination.  You're right I didn't know the difference.

    So a memory resource (such like the Application class as mentioned above) when instantiated resides on the heap.  Therefore, when a method (for example) is done using the instance of the class the GC will take care of destroying that instance on the heap.  However, for a non-memory/unmanaged resource that implements the IDisposable interface, the developer is required to dispose the object from the heap.  Is this correct?

    If I was to create a new class, what resources do I need to use that would require me to implement IDisposable? 

    Wednesday, June 25, 2014 9:18 AM