Game Development Community

system.outofmemoryexception in mscorlib.dll

by David Horn · in Torque X 2D · 07/25/2010 (4:33 pm) · 12 replies

Current TX version 3.1.4.

Hello everybody.

I am currently testing my game and found a big poblem in memory management.

When I run my game long enough,I get an error:
an unhandled exception of type 'system.outofmemoryexception' occurred in mscorlib.dll

This happens if I run my game long enough.

The place where it happens is when it tries to load a new texture.

Specifically in the
Texture tex = cm.Load<Texture>(loadpath);
inside the public Resource<Texture> LoadTexture(string asset) function
in the ResourceManager.cs file.

Is there some way I can fix this - possibly with a try catch or some kind of Dispose functionality somewhere?

Does this have something to do with the long "memory leak" thread?

Thank you!

#1
07/27/2010 (9:22 pm)
@David - Out of memory in TX2D is either...

1) leaking sprites.... bullets fired and fly off the screen and never get killed.

2) your loading too many huge textures.

3) you have custom code (or an engine bug) that isn't disposing things properly.

Can't really help you with any of these without a lot more details.
#2
07/29/2010 (11:20 am)
From the tests made it's the SetTexture bug. Not fixed (not even found) in the 3.1.4, found and fixrd in 3.1.5 Unofficial Repository.
#3
07/29/2010 (2:33 pm)
Yeah - I made the post before I downloaded the Repository.

I'm doing tests now, but I think that was the problem.
#4
08/02/2010 (5:23 pm)
Well it worked perfectly everywhere I'm using the setTexture.

Now I did get a code 3 sometimes when I change the arena textures.

I'm allowing the user to change the textures of certain things. One in particular (the outside mat) is very large in width and height (2448 x 856)

now the code I use to change the texture in real-time is a s follows:

try
            {
                T2DStaticSprite outm = (T2DSceneObject)TorqueObjectDatabase.Instance.FindObject("outmat") as T2DStaticSprite;
                
                SimpleMaterial m9 = new SimpleMaterial();
                m9.TextureFilename = @"data/images/" + outsideMatArray[Game.Arenas[arena][5]];
                outm.Material = m9;
            }
            catch
            {
                //don't do anything
            }

now, the try gets the error, but I'd rather not have the error period.

Two questions...

1. would it help if I added
outm.Material.Dispose();
before I reassign the material?

2. is there a better method to do this in real-time?
#5
08/02/2010 (7:44 pm)
Hi Dave,

why don't you use the SetTexture there as well? Any particular reason?
#6
08/02/2010 (7:47 pm)
How would I use it on a T2DStaticSprite?

I thought SetTexture was only used for 3D texturing?
#7
08/02/2010 (8:05 pm)
Well,

this way:

Resource<Texture> texture = ResourceManager.Instance.LoadTexture(@"data/images/mytexture");
(_myStaticSprite.Material as SimpleMaterial).SetTexture(texture.Instance);

;)
#8
08/02/2010 (8:11 pm)
wow.
that just... couldn't be easier, could it?

:)

thanks, man.

Like you say, code 3's are just as bad as code 4's. This should squash them all.
#9
08/02/2010 (8:23 pm)
Code 3 are even worse as they are way more difficult to track down :) I really hope that this one is the last memory leak... I'll have a look to that Material setting... it doesn't free the previous material but if it's not disposed it means that is still referenced somewhere.

BTW, I don't get why in the T2DStaticSprite the Material is defined as RenderMaterial, not really evident... it's worth some investigation.
#10
08/02/2010 (8:30 pm)
I forgot one thing: if your arena is a LightingMaterial (light enabled) you need to change the above code specifying LightingMaterial instead of SimpleMaterial.
#11
08/02/2010 (11:49 pm)
It's SimpleMaterial. And it works like a charm.

I'll test the code 3 later, but I assume that since I'm using settexture with the latest version, it should be ok. I'm assuming now I don't have to manually dispose of the old material first, right?

I mean, is there any benefit to putting
myStaticSprite.Material.Dispose();
before the setTexture line of code?
#12
08/02/2010 (11:54 pm)
Nope, no need. Actually disposing there the static sprite material might cause a Code 4 ;)