How to call a destructor in C # XNA

I have an object and just want to destroy it at some event. How to call a destructor in XNA?

+6
source share
7 answers

Set the object to null , and the garbage collector will pick it up the next time it starts.

As a side note, if the object is what you create often (enemies, bullets, etc.), then you can use pool instead of deleting the object. This means that the object is recycled, and thus the garbage collector is called less frequently, which increases performance.

+6
source

While your mileage may vary, I prefer to use IDisposable and Dispose() objects that I no longer need. This is especially so. true when you are using unmanaged resources, but setting Dispose() declares the intent.

See this resource in GC.SuppressFinalize for a good example implementation of IDisposable .

http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx

+3
source

Link this object to null .

+1
source

After setting the object to null , if for some reason you want to immediately assemble the object, call GC.Collect() .

0
source

Which object? If it is a one-time / IDisposable, you must call object.Dispose () otherwise you can just use object = null and it will be "cleaned up" automatically. This is not C in which you work;)

If your “object” is actually a complex class or something with a lot of objects in it, you might want to make it an IDisposable class.

0
source

If the object contains unmanaged resources, then implement IDisposable (and use using or call Dispose() on it when you are done with it).

If it does not contain unmanaged resources, the garbage collector will request it "at some point" if it is no longer referenced.

GC.Collect () will collect the garbage collector, and it will "destroy" your object if there are no references to it. This is not a good idea, since it has performance implications (it takes time and promotes other objects to a higher generation, which makes them longer in memory until they are fixed).

Why do you need an object destroyed at a certain fixed time?

0
source

What you want to do is call GC.SuppressFinalize() if you handle the cleanup yourself ... Usually you use GC.SuppressFinalize() within an IDisposable . See this sample code for general use of IDisposable :

http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx

If you really need to collect garbage right away,

 var myObj = new MyObject(); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SupressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(myObj); 

But I warn you that you really must let the object go out of scope and let the GC collect objects in a natural way. .NET runtime is very effective at managing memory until you try to do this work.

EDIT

After looking at the comments, I see that you forgot to leave important information, since you linked this object to other methods of objects. This means that the object you are trying to complete will not complete until the method used to monitor the event completes, thereby storing about a thousand additional objects in memory.

What you can do to break this strong link is to use an object called WeakReference . Or use lambda to break strong links.

See this for an example of using WeakReference http://msdn.microsoft.com/en-us/library/system.weakreference.aspx

Or you can do something like this:

 dym.cas += (x, y, z) => gameTime.ElapsedGameTime(x,y,z); 

instead

 dym.cas += gameTime.ElapsedGameTime; 
0
source

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


All Articles