Destruction of an instance

Asked

Viewed 1,235 times

3

How do I destroy an instance?

I have an object whose instance is referenced in various parts of my code. At some point I need to delete this object and for that I am doing the following:

meuObjeto = null;

The problem is that that code is only setando as null the variable meuObjeto. At the other points of the system, the object still exists. How do I update all references in only one point of the code?

  • I’m not sure it would be possible to do something like that. While some variable is pointing to a particular object it will not be "clean" by Garbage Collector, maybe if you made it implement the interface IDisposable, but it’s hard to say if something like that would be really right for your case without coming the code as a whole.

  • What you need is System.GC.SupressFinalize(Objeto).

1 answer

5


C# is a language with managed memory, so you do not destroy the object, it will be destroyed when there is no reference to it and memory needs its space. The gargabe Collector will take care of this.

It is unnecessary to put null on the object, if it is not being destroyed it is because there are references to it.

Then you just need to stop using the object. Probably the code is holding it improperly. You’d have to scan the code to find out where the bug is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.