-1

I Have this class :

    public class A
    {
//Some logic code here
    }

My question is how I can dispose or destroy any object created based on this class after going out the scope?

For example:

  A a = new A();
    //some code here

after the //some code here I wanna the a object to destroyed or disposed and releasing his allocated memory.

XDev
  • 125
  • 1
  • 8
  • your assumption is wrong: that class ***is*** managed. and if you actually have a class with unmanaged resources: make it [`IDisposable`](https://learn.microsoft.com/en-gb/dotnet/api/system.idisposable?view=net-5.0) and use it with [`using`](https://learn.microsoft.com/en-gb/dotnet/standard/garbage-collection/using-objects). done. (boy, the manuals _really_ have a lot to teach, don't they?) – Franz Gleichmann Jan 19 '21 at 20:16
  • Just enter your question's title into Google: [Cleaning up unmanaged resources](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/unmanaged) – Camilo Terevinto Jan 19 '21 at 20:17
  • GC does this for you in background, if you want to explicitly do it, use GC.Collect() – Krishna Chaitanya Jan 19 '21 at 20:18
  • @KrishnaChaitanya GC has no knowledge about *unamanaged* resources, otherwise they wouldn't be called that way – Camilo Terevinto Jan 19 '21 at 20:19
  • Note that *encapsulation* means that the clean up should happen in the class when it is destroyed/deallocated not externally. Also, the code *usually* uses an object for sometime after it has been created; disposing of it on the next line would be suboptimal. – Ňɏssa Pøngjǣrdenlarp Jan 19 '21 at 20:22
  • If you neglect to Dispose of your unmanaged resources correctly, the garbage collector will eventually handle it for you when the object containing that resource is garbage collected @CamiloTerevinto – Krishna Chaitanya Jan 19 '21 at 20:23
  • @KrishnaChaitanya your definition of "unmanaged resources" seem to be very unusual. `FileStream` is *managed* resource that should be disposed and will be picked up by GC if not disposed properly.... File `HANDLE` (as in Win32 concept) is related *unmanaged* resource that does not have any `Dispose` calls by itself and GC will not do anything to it. – Alexei Levenkov Jan 19 '21 at 21:53
  • Closing as duplicate for question in the title - https://stackoverflow.com/questions/16646969/unmanaged-resources-and-dispose. Body of the post seem to be only loosely related to the title and answered here... but for future readers who likely get to this by search for title duplicate is far more relevant. – Alexei Levenkov Jan 19 '21 at 22:05

1 Answers1

1

I'll ignore the title and concentrate on the body of the question since it seems evident from the question what the issue is.

The short answer: You don't. This will be taken care of automatically by the garbage collector when it deems it necessary. (You don't need to run the garbage collector. That happens automatically.)

The garbage collector then looks for entities that have gone out of scope, like a in your example, and will free the space they used. It will then see that the internals of a have also, obviously, gone out of scope (unless they are referenced elsewhere in your code), and handle them.

Having said that, when you use variables of classes that implement Dispose it's best to call Dispose explicitly or use using blocks to get that done. You can also implement a finalizer which will be executed when a gets disposed. But it seems like that's not what you were asking about.

ispiro
  • 26,556
  • 38
  • 136
  • 291