Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

SpriteFont with BMFont fonts seems to leak after being disposed. #167

Closed
ghost opened this issue Sep 24, 2013 · 5 comments
Closed

SpriteFont with BMFont fonts seems to leak after being disposed. #167

ghost opened this issue Sep 24, 2013 · 5 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Sep 24, 2013

Hi, I havent found the exact cause yet, but if you do this over and over again.

        var font = SpriteFont.Load(graphicsDeviceManager.GraphicsDevice, "arial.fnt");
        font.Dispose();

Then the GPU memory increases. (Managed heap memory also increases but only by a little)

Using NVIDIA Nsight, I found that D3D11Texture2D without ShaderResourceView remain allocated.

I'll update this issue once I find more details.

@ghost ghost assigned ArtiomCiumac Sep 24, 2013
@ghost
Copy link
Author

ghost commented Sep 24, 2013

maybe in SpriteFont.cs#L166 the Texture2D being loaded and assigned to bitmapName is not being disposed?

@ghost
Copy link
Author

ghost commented Sep 24, 2013

i could quickfix it by explicitly disposing all textures like this:

    protected override void Dispose(bool disposeManagedResources)
    {
        if(disposeManagedResources)
        {
            for (int i = 0; i < textures.Length; i++)
            {
                textures[i].Dispose();
            }
        }
        base.Dispose(disposeManagedResources);
    }

but i guess, this does not follow the SharpDX coding style ;-)

@ArtiomCiumac
Copy link
Contributor

Here are the results of my analysis so far:
The anonymous method from the SpriteFont.cs#L166 is passed around and gets called at SpriteFontData.cs#L119 - here the leaked texture gets created and it is used in SpriteFont.cs#L227.

The first location is a static context - so we can't wrap it in ToDispose, second location is not suitable too as SpriteFontData is not disposable, so the only point where the texture can be registered for disposal - is the last location. But the problem is that the same SpriteFontData potentially can be reused to create several SpriteFont object, and disposal of one will invalidate the others which share the same glyph texture.

The issue can be reproduced only in case when SpriteFont is loaded manually - when it is loaded via ContentManager it will dispose the resources correctly, so until this will be fixed - load fonts via myGame.Content.Load<SpriteFont>(...);.

@ghost
Copy link
Author

ghost commented Sep 24, 2013

thanks for the info!

when does it make sense to create another SpriteFont object with the same SpriteFontData?

due to the architecture of my project, i cant use ContentManager.

offtopic: how do you make these links to the sourcecode lines in github?

ArtiomCiumac added a commit that referenced this issue Sep 24, 2013
…dispose unmanaged resources from SpriteFontData.
@ArtiomCiumac
Copy link
Contributor

The issue should be fixed now. If developer will decide to load SpriteFontData manually - it is his responsibility to dispose correctly the resources (or it can pass the flag as true - look a the commit above).

when does it make sense to create another SpriteFont object with the same SpriteFontData?

It all depends on the developer, we just need to make sure that it will behave as expected. This could be used to load some custom font format with variable properties, for example.

offtopic: how do you make these links to the sourcecode lines in github?

Format it as markdown link.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant