Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShapeExtensions (FillRectangle) texture disposed in android when app is not foreground #769

Open
NebulaSleuth opened this issue Jun 14, 2022 · 1 comment
Labels
Milestone

Comments

@NebulaSleuth
Copy link

Sometimes the texture created for ShapeExtensions gets disposed when the application is pushed to the background. When the app comes back to the foreground, the shapes no longer draw properly (all black).

I can't find any method to clear the internal 1x1 texture to force it to regenerate it.

@captkirk88
Copy link

captkirk88 commented Aug 18, 2024

It happens when the app on Android is not on the foreground, where the user is actively interacting with the app. The android activity may stop but the application is still running so dalvik (the android vm) will clear memory and the memory it clears may be the texture you wanted to keep alive.

You could run the https://square.github.io/leakcanary/ to see the leaks in action.

MonoGame should be preventing this by default for it's android port. I am pretty sure this is a issue internally to MonoGame since I reviewed the MonoGame.Extended source just to see if this would happen to me and

        private static Texture2D GetTexture(SpriteBatch spriteBatch)
        {
            if (_whitePixelTexture == null)
            {
                _whitePixelTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
                _whitePixelTexture.SetData(new[] { Color.White });
                spriteBatch.Disposing += (sender, args) =>
                {
                    _whitePixelTexture?.Dispose();
                    _whitePixelTexture = null;
                };
            }

            return _whitePixelTexture;
        }

shows that FillRectangle always calls that method resulting in a null check for _whitePixelTexture in which the _whitePixelTexture is then created again.

I should note, I don't use MonoGame. I use the fork which is preferable for me considering the 600+ issues in the MonoGame repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants