XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Dispose() a class, do I need it in this situation?

Last post 11/15/2009 10:38 PM by Glaucous. 6 replies.
  • 11/15/2009 4:05 PM

    Dispose() a class, do I need it in this situation?

    Hello.

    I'm reading up on destructors and dispose of classes and objects at the moment. Somewhat a pain in the ass, but oh well.
    I'm creating a 2D-Game, which has (among other classes)  a baseclass Level and a couple of subclasses for each level.

    The problem here is that I create these classes as soon as the game starts, which is quite unnecessary since I load a lot of Texture2D, which just uses additional RAM. So what I need to do is, as soon as I start a new level in the game, I will destroy/dispose all the other levels.

    I do not know exactly how the garbage collector works, but I don't think it will fix this automatically. So how to I do this the easiest way?
  • 11/15/2009 5:18 PM In reply to

    Re: Dispose() a class, do I need it in this situation?

    Answer
    Reply Quote
    As the ContentManager caches your Texture2D's, it would probebly be easiest to use a new ContentManager for each Level, and Dispose() the old one (and thus release all the textures) when you finish a level. For this to work well, you'd also have to load levels on demand and dispose of them when used.
  • 11/15/2009 7:35 PM In reply to

    Re: Dispose() a class, do I need it in this situation?

    Ah great, they use that in the Platformer Game which ships with XNA Game Studio.
    However, I must ask. If the LEVEL subclass creates an enemy class, when I Dispose() the Level subclass, with the enemy class dispose as well? Since I send the same ContentManager to the enemy class, as the level subclass.

    Makes sense?
  • 11/15/2009 8:29 PM In reply to

    Re: Dispose() a class, do I need it in this situation?

    Answer
    Reply Quote
    I'm no expert at this, but i would say that if you no longer have any references to your level, and it is the only thing that directly reference the enemy, then they will both be collected by the Garbage Collector as they can no longer be reached.

    Im not sure if you mean 'with' or 'will', but it should happen so that when the ContentManager is Dispose'd, content is only referenced by the level/enemies (although I think the content is unusable now), then the level's reference from the main program is removed so it and all the enemies and their members/content should be collected in the next Garbage Collection. I  don't think the Enemy class itself needs to dispose as disposing is meant for unmanaged resources, and the ContentManager disposes of all the unmanaged stuff it references when it is disposed.

    In short I think all you need to remember is if you want something removed then unload anything from the content manager then remove the references so it is no longer attached to the Main part of the program.


    However, I'm sure one of the MVP's is going to come along and tell me how wrong I am.
  • 11/15/2009 8:45 PM In reply to

    Re: Dispose() a class, do I need it in this situation?

    Yeah, sorry, I meant "will". A little bit tired and sick at the moment.
    Okay, well it seems to be working quiet well actually. The memory usage was at 80 MB before, now it's around 40-45 depending on which level.

    Thanks for the help!
  • 11/15/2009 8:48 PM In reply to

    Re: Dispose() a class, do I need it in this situation?

    You should read this link Taking out the Trash

    To cleanup content, you shouldn't need to call Dispose at all, and you don't need finalizers.

    Just use the Unload method on the ContentManager, and use multiple ContentManagers to manage the memory usage.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 11/15/2009 10:38 PM In reply to

    Re: Dispose() a class, do I need it in this situation?

    Works very well, I actually did almost exactly what that post recommended.
Page 1 of 1 (7 items) Previous Next