Content.Unload(); NOT unloading or disposing in Monogame for Windows 8

Hi, I narrowed my issue with strange sounds and crashes down to the possible issue that content.Unload() isn’t getting the job done for me in MonoGame. I have loaded the screens that caused the crashes first and they load fine, but when I run my game through as normal the crashes usually occur after about 3 or 4 game screens.

I seen this same issue addressed on GitHub over a year ago and was wondering if it was ever resolved? Why am I still experiencing the same issue on Windows 8? Is there a work around? I looked at the source code, but I am using the latest stable build of MonoGame 3.0. Wasn’t the ContentManager.cs file updated and fixed to properly dispose of user-created contentManagers?

Thanks!

Content.Unload seems to be working correctly. The only problem I know about is that currently SharpDX platforms (which include Windows 8) to not dispose textures correctly unless Dispose or Content.Unload is called (see this).

Hi Nezz!

Thanks for the help, but it says SharpDX.Utilities doesn’t contain a definition for ‘Dispose’. My set up is as such…

class TitScreen : GameScreen
{
ContentManager content;

    public override void LoadContent()
    {
        if (content == null)
            content = new ContentManager(ScreenManager.Game.Services, "Content");

         //load all of my content
    }

public override void UnloadContent()
{
content.Unload();
}

I made GameScreen IDisposable and added a virtual method for Dispose and then override it in my derived class like this…

protected override void Dispose(bool disposing)
{
//initially had this…
content.Dispose();

//tried your code here, but it didn’t work
}

How would I properly go about diposing here, so that I know that I am doing it correctly, because I have always used content.Unload() on Windows and Windows Phone and never had a problem, if this is truly my problem.

I have read up on Dispose(), but my implementation might be flawed here!
Per Tom in another post about SharpDX, there is a way to tell SharpDX to finalize for us, but I don’t even know how to do that.

thanks!

Thanks!

Ooops, I call Dispose(); in the UnloadContent(), by the way!!

public override void UnloadContent()
{
Dispose();
}

Thanks Nezz, but I have come at it from different angles and I’m still getting the same result. I have read ALL of your other posts about content.Unload and the use of dispose() and I must say that you were right in your issue #843. I believe someone was arguing you down about the proper way to dispose of content, and after day in and day out research of my own…I have settle my beliefs that in XNA the only proper way to dispose of content properly is to call Content.Unload();

Each instance of ContentManager will only load any given resource once. The second time you ask for a resource, it will return the same instance that it returned last time.
ReferenceEquals(Content.Load(“something”),
Content.Load(“something”)) == true

To do this, ContentManager maintains a list of all the content it has loaded internally. This list prevents the garbage collector from cleaning up those resources - even if you are not using them.

To unload the resources and clear that internal list, call ContentManager.Unload. This will free up the memory the loaded resources were using. Now if you ask for the same resource again - it will be re-loaded.

Of course, if you are using those resources when you call Unload, all of those shared instances that you loaded will be disposed and unusable.

Finally, don’t call Dispose on anything that comes out of ContentManager.Load, as this will break all the instances that are being shared and cause problems when ContentManager tries to dispose of them in Unload later on.

Now, per other comments that I have read in other post…they staunchly deny that we should unload content this way and say that we should be calling Dispose() explicitly.

Anyways, I rarely see anyone’s issues or problems receive any real resolutions, so I guess that I am completely on my own in my expectations that the same general XNA behaviors work in MonoGame. I hope this community becomes more user-friendly as they continue to expand it, because as it is now, you either have to be as smart as the creators, or your dead in the water!

Thanks again!

I’m kinda confused right now. Are you saying that ContentManager.Unload() doesn’t work, but ContentManager.Dispose() works properly?

Hi Nezz!

I was just rambling about the issue you raised about a year ago about the proper way to dispose of content. It was stated by someone that using Content.Unload(); wasn’t the proper way, but I’ve seen many articles that says otherwise. I’m just frustrated because I’m getting a weird crash that occurs randomly after 3 to 4 game screens have been loaded and unloaded. Content.Unload is working I believe, but my debugger doesn’t attach, so I get nothing in the call stack…just this in the…

Debug output window:

‘SinisterPop.exe’ (CLR v4.0.30319: Immersive Application Domain): Loaded ‘C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll’. Cannot find or open the PDB file.
A first chance exception of type ‘System.IO.FileNotFoundException’ occurred in mscorlib.dll
A first chance exception of type ‘System.Exception’ occurred in SinisterPop.exe
WinRT information: The operation attempted to access data outside the valid range

The program ‘[4920] SinisterPop.exe’ has exited with code -1073741819 (0xc0000005) ‘Access violation’.

amongst a bunch of others…

That debug output doesn’t look anything like out of memory. Notice that MonoGame isn’t even in the stack trace. I’m sure this is a bug in your saving system that is related to the WinRT IO and would occur if you didn’t load any content for the game screens (or maybe it occurs because monogame is also trying to read files?).

Screen manager rumor: It’s about the structure of it in stacks or the structure of it in inheritance based singleton.

those are all false.

the #1 thing that matters is memory management. If your not a fool who doesn’t know how to call dispose then the answer is restructure your state management to dispose and manage memory properly. Luckily I saw your question as it’s true form.

Interesting response!

I don’t know whether to be insulted or thankful by your response, but you sound like a very knowledgeable user that other less knowledgeable users could benefit from. Please explain the true nature of my question, so I can figure out if indeed, the old Microsoft state management classes DO need restructuring or not(NO riddles). If so, am I to assume that you have ran into the same issues while using MonoGame and have resolved them by reconfiguring what use to work on Windows Phone and Windows to work in MonoGame!? Lastly, just calling content.Dispose(); should in-fact dispose of any and all content, yet my problem persists, so I’m not even sure if it is a memory issue.

oh.
false internet nonsense aside. Please respond in a way that I can help you. If you do, i can continue. if not, i cannot.

I am doing this for myself really just posting responses on the internet for monogames/xna because it’s fun.

JESUS IS GOD! JESUS IS LORD!

UPDATE!

Hi everyone! I just wanted to say thanks for all of the replies to this question/issue, and that we can consider this issue closed!

My apologies, as none the issues listed were the actual culprit or reasons for my crashes. My issue has been resolved and the content.Unload() IS and always has been working in MonoGame!

The issue was the amount of content that I was trying to load between levels causing my app to hang and ultimately exited by the system!

Thanks again!

MONOGAME ROCKS!