views:

370

answers:

4

XNA games have an Unload() method, where content is supposed to be unloaded. But what is the point of this? If all the content is being unloaded, then the game must be exiting, in which case everything would be garbage collected anyway, right?

+2  A: 

It’s always polite to clean up after yourself… otherwise people will stop letting you play with their toys.

My best guess is that it would allow you to nest Game objects into your project and give you a way to clean them up later. This would allow for better reuse of your code. Hopefully an XNA MPV or someone from the XNA team will find this and provide more insight.

Matthew Whited
What does it mean to "next" a `Game` object?
Rosarch
It means I fat fingered 'nest'... thanks, I'll fix it.
Matthew Whited
Even then it's not so useful, after all once the game object is garbage collected the content manager will be collected and thus so will all content it was managing.
Martin
Unless the content manager has some sort of static cache built in that would keep a reference to any loaded content even after you close the game object. If you have complex levels it could also be useful to make each level unload all of its related content to reduce the stock pile of crap you leave in memory. Games and game console are not your typical computer app. They need to be much cleaner and much more responsive.
Matthew Whited
+4  A: 

As far as I understand it, it's not useful for any standard uses, since as you say the garbage collector deals with things for you.

However, it's useful to have an event called when your game is exiting for many things. For example you could send a message to all clients in a multiplayer game telling them you're exiting, and then you can let the garbage collector kill your network connections.

Martin
Just because all of your objects will be unloaded when you close your program doesn't mean you shouldn't use `IDisposable.Dispose()` or `using (...)` either.
Matthew Whited
That's a good point.
Martin
A: 

Its to unload some texture as C# can have issues with its garbage collector.

A: 

Unload is sometimes necessary, especially when working with libraries that don't get unloaded via the Garbage Collector. One example is XACT. I remember having to destroy the object responsible for XACT music in the Unload event in one particular case where the object was not being destroyed by the GC... But I forgot the actual scenario...

More importantly however, the UnloadContent method for GameComponent classes instead of Game classes. Generally, GameComponent and DrawableGameComponent objects can make use of several resources. When the object is destroyed, you have to make sure that all resources related to the object are destroyed, releases or sometimes put in pending state.

I would suggest you leave it for now. As you move forward with XNA development, and for larger scale projects, you might need to make use of the Unload method, and understand the certain cases where you need.

SiN