bryanedds

Hi all,

I create and add my GameComponents to Game in the order they need to be created. But somtiems, things go wrong when my custom GameComponents get disposed. They get their Dispose() method called in the same order that their Initialize() is called. This is not how such things are supposed to work from my experience. Destruction should always happen in the opposite order of creation / initialization.

Perhaps I'm not supposed to override the non-virtual Dispose function of the GameComponent class (I use IDisposable and the new keyword in the function signature to do this since... well, I need to define a Dispose for my own purposes). But that brings me to my second issue - why isn't GameComponent.Dispose virtualized A simple oversight perhaps

Anyways, these are some weird issues. I can sort of work around them, but it's a mess. Someone let me know if I'm completely out of line here. If not, I'll submit something to Connect.




Re: XNA Game Studio Express Shouldn't GameComponents be Disposed in the opposite order they are Initialized?

drawle

This is a managed code issue and not XNA specific. The garbage collector does not guarantee any particular order for releasing managed resources as far as I know. Finalize and Dispose are not destructors and don't behave at all like C++ destructors. If you need to enforce an order you should manually call Dispose and call GC.SuppressFinalization in Dispose to prevent a second call to it.

A good reference is the MSDN article:

Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework





Re: XNA Game Studio Express Shouldn't GameComponents be Disposed in the opposite order they are Initialized?

bryanedds

Dispose is not called by the garbage collector... it must be called manually. Which means there is some code that is calling Dispose for Game's GameComponents in the same order as it calls Initialize.

Meaning, somewhere in Microsoft's base Game class, there is code approximate to this -

void Dispose()

{

   foreach (GameComponent comp in components)

   {

      comp.Dispose();

   }

}

Which should actually be this -

void Dispose()

{

   for (int i = components.Count -1; i > -1; i--)

   {

      components[ i ].Dispose();

   }

}

So it is an XNA issue I believe.






Re: XNA Game Studio Express Shouldn't GameComponents be Disposed in the opposite order they are Initialized?

Stephen Styrchak - MSFT

You should not replace the GameComponent implementation of IDisposable with your own. You should override the protected Dispose method with a boolean parameter.

eg,

protected override void Dispose(bool disposing)
{
if (disposing)
{
//TODO: dispose managed resources
}
//TODO: dispose native/unmanaged resources
base.Dispose(disposing);
}

This implementation is part of a pattern that is recommended as a best practice for Dispose implementation. For details, please read this article.

http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconImplementingDisposeMethod.asp

The point of the boolean is to let you know whether Dispose(bool) is being invoked by the IDisposable.Dispose method, or by the finalizer. If it is the finalizer, then you should not attempt to access or dispose any managed objects/resources from your class. However, you still need to release native resources if you have any.

--Stephen





Re: XNA Game Studio Express Shouldn't GameComponents be Disposed in the opposite order they are Initialized?

bryanedds

Thank you Stephen! I was wondering why there was so much difficulty in making my Dispose methods.

I don't know if that will solve my issue in particular, but it is a great help nontheless!