如何在C# XNA中调用析构函数

发布于 2024-11-14 14:13:19 字数 41 浏览 2 评论 0原文

我有一个对象,只是想在某些事件中销毁它。如何在XNA中调用析构函数?

I have a object and simply want to destroy it on some event. How to call the destructor in XNA?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

多情癖 2024-11-21 14:13:19

将对象设置为 null,垃圾收集器将在下次运行时拾取它。

附带说明一下,如果该对象是您经常创建的对象(敌人、子弹等),那么您可能需要使用 pool 而不是删除对象。这意味着对象被回收,因此垃圾收集器的调用频率会降低,从而提高性能。

Set the object to null and the Garbage Collector will pick it up on it's next run.

As a side note, if the object is something that you create often (enemies, bullets etc..), then you may want to use a pool instead of deleting the object. This would mean that object is recycled, and thus, the garbage collector is called less often which will increase performance.

早乙女 2024-11-21 14:13:19

虽然您的情况可能有所不同,但我的偏好是使用我不再需要的对象的 IDisposableDispose() 。这是特别是。当您使用非托管资源时为 true,但设置 Dispose() 会声明意图。

请参阅 GC.SuppressFinalize 上的资源,了解如何实现 IDisposable 的好示例。

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

While your mileage may vary, my preference is to use IDisposable and Dispose() of objects I no longer need. This is esp. true when you are using unmanaged resources, but having the Dispose() set up declares intent.

See this resource on GC.SuppressFinalize for a good example of how to implement IDisposable.

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

无声静候 2024-11-21 14:13:19

将此对象的引用设置为null

Set reference to this object to null.

余生一个溪 2024-11-21 14:13:19

将对象设置为 null 后,如果您出于某种原因希望立即收集该对象,请调用 GC.Collect()

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

究竟谁懂我的在乎 2024-11-21 14:13:19

什么样的物体?如果是 Disposable/IDisposable 你应该调用 object.Dispose()
否则,您可以只使用 object=null,它将被自动“清理”。这不是您正在使用的 C 语言;)

如果您的“对象”实际上是一个复杂的类或其中包含更多对象的类,您可能需要考虑将其设为 IDisposable 类。

What kind of object? If is an Disposable/IDisposable you should call object.Dispose()
Otherwise, you can just use object=null, and it will be 'cleaned' automatically. This is not C you're working in ;)

If your 'object' is actually a complex class or something with more objects in it, you might want to consider making it an IDisposable class.

じее 2024-11-21 14:13:19

如果该对象拥有任何非托管资源,则实现 IDisposable (并在使用完毕后使用 using 或调用 Dispose() ) 。

如果它不持有非托管资源,那么当不再有对其的引用时,垃圾收集器将在“某个时刻”声明它。

GC.Collect() 将使垃圾收集器进行收集,如果没有对它的引用,它将“销毁”您的对象。这不是一个很好的主意,因为它会影响性能(它需要时间并将其他对象提升到更高的一代,使它们在被回收之前在内存中持续更长时间)。

为什么需要在某个固定时间销毁对象?

If the object holds any unmanaged resources then implement IDisposable (and use using or call Dispose() on it when you're done with it).

If it doesn't hold unmanaged resources then the garbage collector will claim it "at some point" when there are no more references to it.

GC.Collect() will make the garbage collector collect and it will "destroy" your object if there are no references to it. This is not a very good idea since it has performance implications (it takes time and promotes other objects into a higher generation making them last longer in memory before being reclaimed).

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

阳光下的泡沫是彩色的 2024-11-21 14:13:19

您想要做的是调用 GC.SuppressFinalize()如果您自己处理清理...通常您在IDisposable类中使用GC.SuppressFinalize()。有关 IDisposable 的常见用法,请参阅此代码示例:

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

如果您确实需要立即进行垃圾收集:

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);

但我警告您,您确实应该让对象超出范围,让GC自然地收集对象。只要您不尝试完成它的工作,.NET 运行时在管理内存方面就非常高效。

编辑

查看评论后,我发现您忘记留下一条重要信息,因为您将有问题的对象绑定到其他对象方法。这意味着您尝试最终确定的对象在用于监视事件的方法完成之前不会最终确定,从而在内存中保留大量额外的对象。

要打破这种强引用,您可以使用名为 的对象弱引用。或者使用 lambda 来打破强引用。

有关使用 Wea​​kReference 的示例,请参阅此内容
http://msdn.microsoft.com/en-us/library/system .weakreference.aspx

或者你可以这样做:

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

而不是

dym.cas += gameTime.ElapsedGameTime;

What you want to do is call GC.SuppressFinalize() if you are handling the cleanup yourself... Usually you use GC.SuppressFinalize() within an IDisposable class. See this code example for the common use of IDisposable:

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

If you really need to have it Garbage Collected 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 caution you that you should really let the object go out of scope, and let the GC collect the objects naturally. The .NET runtime is very efficient at managing memory as long as you don't try to do it's job.

EDIT

After reviewing the comments, I see you forgot to leave an important piece of information, in that you were binding the object in question to other objects methods. This means that the object that you are trying to finalize is not going to finalize until the method used to watch the event has finalized, thus keeping around a ton of extra objects in memory.

What you can do to break this strong reference is use an object called WeakReference. Or use lambdas to to break the strong references.

See this for an example of using a 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 of

dym.cas += gameTime.ElapsedGameTime;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文