0
\$\begingroup\$

The problem I have discovered is most likely between classes, so I dont really have much option but to post the whole code here: https://www.dropbox.com/sh/smyw9t1ap3by2vd/pBX-aJJ-Qx

I have been having some trouble with spritebatches rendering textures recently using monogame for windows 8 with xaml where it throws an error after a few seconds of running. The error only occurs after a few seconds, though the less efficient or more textures i render, the quicker it crashes. After doing a git submodule update, the error message is entirely gone, but the textures still dissappear from the screen leaving just the cornflower blue.

The folks on GitHub have decided that this is a problem with my code and not theirs, which means I am definitely doing something wrong. For reference, here is the issue thread: https://github.com/mono/MonoGame/issues/1927. Thanks for any help!

UPDATE: I have identified the source of the problem. It has to do with declaring and holding the texture.

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        ContentManager myManager = new ContentManager(Content.ServiceProvider, Content.RootDirectory);
        terrainEngine.Load(myManager);
    }

    ContentManager content;
    public void Load(ContentManager content)
    {
        this.content = content;
        blockTextures = content.Load<Texture2D>("Graphics\\Blocks");
        spriteFont = content.Load<SpriteFont>("Graphics\\SpriteFont1");
    }

Now, if I call Load(content) from inside the terrainEngine class Update method, then the program works, otherwise, the textures disappear after a second.

\$\endgroup\$
  • \$\begingroup\$ Instead of linking to all your code, please include only the code that's causing the issue and the exact error message you're getting. \$\endgroup\$ – MichaelHouse Aug 13 '13 at 23:12
  • \$\begingroup\$ Sorry, but I spent hours trying to do that for the guys at github, I just have no idea where the problem is. The best I can tell is that its in-between the classes, but I have no idea where. \$\endgroup\$ – Jonheel Aug 13 '13 at 23:14
  • \$\begingroup\$ Nice work finding the problem. I imagine it was because you were declaring variables inside a scope that eventually got cleaned up. \$\endgroup\$ – MichaelHouse Aug 14 '13 at 0:35
  • \$\begingroup\$ declaring variables inside a scope? could you explain, because my solution of reloading the assets every update method will not scale up. I think you're saying that I declared the Texture2D variable inside the Load method, but declaring it in the constructor first didn't fix it earlier. \$\endgroup\$ – Jonheel Aug 14 '13 at 0:38
  • 1
    \$\begingroup\$ yep, thats exactly right, I should have just used Content. StackExchange won't let me post an answer to my question for the first 8 hours, and you deserve the credit anyway, want to post the answer? \$\endgroup\$ – Jonheel Aug 14 '13 at 0:44
1
\$\begingroup\$

I'm thinking you're declaring something that's inside a scope that's getting cleaned up with the garbage collector (which explains why using more memory made it happen faster and explains the delay between declaring the textures and them disappearing).

Specifically, I think it's your ContentManager, declared inside your LoadContent() function:

protected override void LoadContent()
{
    _spriteBatch = new SpriteBatch(GraphicsDevice);
    ContentManager myManager = new ContentManager(Content.ServiceProvider, Content.RootDirectory);
    terrainEngine.Load(myManager);
}

Even though you're setting assigning it to a global variable later:

ContentManager content;
public void Load(ContentManager content)
{
    this.content = content;
    blockTextures = content.Load<Texture2D>("Graphics\\Blocks");
    spriteFont = content.Load<SpriteFont>("Graphics\\SpriteFont1");
}

Now that global variable is just assigned to something that's going to get cleaned up when it leaves scope.

Try assigning directly to your global variable:

protected override void LoadContent()
{
    _spriteBatch = new SpriteBatch(GraphicsDevice);
    content = new ContentManager(Content.ServiceProvider, Content.RootDirectory);
    terrainEngine.Load();
}

public void Load()
{
    blockTextures = content.Load<Texture2D>("Graphics\\Blocks");
    spriteFont = content.Load<SpriteFont>("Graphics\\SpriteFont1");
}
\$\endgroup\$
  • \$\begingroup\$ Hey, thanks again for your help, because this got mixed in with an actual monogame problem early on, I was dealing with this simple problem for days. You guys here are awesome \$\endgroup\$ – Jonheel Aug 14 '13 at 1:00
  • \$\begingroup\$ No problem. As you can see, it helps a lot when you narrow down the problem. People rarely want to go digging through all of someones source code to find a problem. Showing that you've done the leg work to track down an issue goes a long way. \$\endgroup\$ – MichaelHouse Aug 14 '13 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.