Deleting Objects in Destructor

I have an object that has a one-time object as a member.

public class MyClass { private MyDisposableMember member; public DoSomething { using (member = new MyDisposableMember()) { // Blah... } } } 

There can be many methods in MyClass that require a using statement. But what if I did it instead?

 public class MyClass { private MyDisposableMember member = new MyDisposableMember(); public DoSomething { // Do things with member :) } ~MyClass() { member.Dispose(); } } 

As you can see, member is in the destructor. Will this work? Are there any problems with this approach?

+4
source share
6 answers

Ideally, Dispose () should have been called before completion. It would be better to follow a typical dispose pattern and allow the user to properly dispose of the object () and have a Dispose finalizer if dispose has not yet been called.

In this case, since you are encapsulating IDisposable, you really don't need to implement a finalizer at all. (At the time of finalization, your encapsulated member will be completed, so there is no need to modify your object - it just adds service data.) For more details, see this blog post I wrote for IDisposable encapsulation.

+9
source

You should probably make MyClass implementation of IDisposable . Inside the Dispose() method, call member.Dispose(); . Thus, the programmer can have control over when the item will be deleted.

+7
source

DON'T DO IT!

GC will do this for you (indirectly, since the object to be deleted or another will contain a destructor)

MyDisposableMember can even be disposed of by the GC before you even destroy it - what happens may not be the way you planned.

Even worse: adding a destructor (or finalizer) to the class takes extra time to dispose of the object (much more time, since the object will remain in memory for at least one collection cycle and, possibly, even be advanced to the next generation).

Thus, it would be completely useless and even the opposite.

+3
source

In your first example, the element is not part of the state of the object, because you create it every time it is used and delete it immediately after. Since it is not part of the state, do not model it as such, just use a local variable when necessary.

More generally, you should put all the removal logic in Dispose () and implement IDisposable, and then use the class along with using or try-finally

+1
source

The only thing that I see wrong (and this is not an error) is the fact that in the using statement you explicitly dispose of the object at that moment in time (when the function / method is called). The destructor cannot be called; they are called automatically. Therefore, at the moment, it may take some time to remove a member. It is better to implement the IDisposeable interface for MyClass.

0
source

Following the Microsoft pattern is your best bet, so users in your class will have full control over when it will be removed.

 public class MyClass : IDisposable { private MyDisposableMember member = new MyDisposableMember(); public DoSomething { // Do things with member :) } ~MyClass() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) // Release managed resources { member.Dispose(); } // Release unmanaged resources } } 
0
source

Source: https://habr.com/ru/post/1302775/


All Articles