# Monogame/SharpDX Texture2D doesn't hold value

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);
}

ContentManager content;
{
this.content = content;
}


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

• 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. – MichaelHouse Aug 13 '13 at 23:12
• 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. – Jonheel Aug 13 '13 at 23:14
• Nice work finding the problem. I imagine it was because you were declaring variables inside a scope that eventually got cleaned up. – MichaelHouse Aug 14 '13 at 0:35
• 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. – Jonheel Aug 14 '13 at 0:38
• 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? – Jonheel Aug 14 '13 at 0:44

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);
}


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

ContentManager content;
{
this.content = content;
}


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);