Game1 Constructor Called Twice, Crashes Game

Hey everyone,

I’m using UWP on Xbox One to ship an ID@Xbox game. I was in the middle of testing if my game can handle exiting to the Xbox home screen and signing out when I found a really weird error where my Game1() constructor gets called twice. I’m not sure what to make of this! Here are my steps:

  1. I launch my game from my laptop, sign in an XboxLiveUser and play a bit of my game.
  2. Press Xbox Home, go to My Game & Apps.
  3. Sign out of the current gamertag, sign in on another one
  4. Resume my game
  5. Game1() constructor gets called again and crashes the game when it tries to create a new GraphicsDevice because there’s already an instance of GraphicsDevice created from the first time Game1() was called.

Any ideas what’s causing this? This doesn’t seem like correct behavior to me.

Michael

Hey, I’d really appreciate some help if anyone has insight on this. If I don’t fix this I will fail certification.

This is definitely linked to Xbox suspending my game when I minimize it and go to other screens… I’m not doing anything special to handle my game’s suspension, but it’s strange to me that it’s calling Game1’s constructor again. There’s a document on handling app suspensions here: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/suspend-an-app

@nkast, in my other thread you mentioned things being suspended. Do I need to do something special here? Sounds like you may have ran into something similar.

Thanks,

Michael

Ok, long story short… if you’re a Single User UWP Application, this requirement doesn’t apply… so I can basically ignore this. However, if in the future anyone is shipping a Multiple User UWP Application, this is definitely an issue.

I’m not sure who works on the UWP branch of things, but they may be interested to know about this for future versions of MonoGame.

Thanks,

Michael

Unfortunately, I haven’t used UWP so I’m not sure what’s going on. Which version of MonoGame are you using, and does it happen if you update to a develop version (if you’re not already on one)? As a temporary fix you can try something like this:

bool ConstructorCalled = false;

Set that to true at the end of the constructor, and return at the start of the constructor if the value is already true.

The constructor should not be getting called again after suspension, though. If all else fails, open an issue here with details and see if those more knowledgeable than I am would know the root cause.

EDIT: I realized the temporary fix wouldn’t work. Have you checked the stack trace when it crashes to see when the constructor is called the second time?

It’s been some time since I worked on Store apps, Things were changing constantly back then.
I took a break from UWP until everything settle down.

What’s a Multiple User UWP and why do we need it in a game?
Is it required for ID@Xbox?

This! VS has (had?) a button to suspend/resume store apps so that your can debug it’s life-cycle.
The best place to look for would be in App.xaml.cs. There’s already a check there to skip re-initialization of MainXamlPage() in case of a re-Launched application.

Keep us updated with your finding.

I used an installer created from the develop branch in early December 2017.

So I can definitely say that when the constructor gets called again it’s like a whole new instance of my game has started. All of the values are null / 0. Even the GraphicsDevice is null, but upon creating it I get a crash that mentions that the GraphicsDevice has already been started on the system.

Yes, unfortunately there’s nothing here that’s useful… I can’t go to any code before the constructor. The stack trace literally looks like when you first start a game and hit the Game1() constructor.

My suspicion is that there’s an Active event that MonoGame is hooked into to start the game… but that same event seems to be fired again when the game resumes from suspension which is causing this to happen. I have no evidence of this, but I can definitely say this isn’t coming from my code.

All UWP games default as a Single User Application, but you have the option to opt in as a Multiple User Application. If you’re making a multiplayer game you’ll have to do this, because it’s the only way to have more than one Xbox profile interact with your game. When you swap over to Multiple User Application the game behaves differently, and you get several more Xbox Requirements you have to meet for certification because of this. I had originally opted in as a Multiple User Application because I wanted the ability to Switch Users from the main menu, but I can totally get by as a Single User Application and plan to do that so I can get around this test case.