简体   繁体   中英

OpenTK.Graphics.GraphicsContextException after a few seconds of running

The error I get is "An unhandled exception of type 'OpenTK.Graphics.GraphicsContextException' occurred in OpenTK.dll

Additional information: Failed to make context 131072 current. Error: 0"

I've searched around and I can't find what's causing it and I'm not sure on where to start if I wanted to fix it myself. The code that I believe is causing it is:

    public static Texture2D GetRectangle(int width, int height, Color colour, bool fill)
    {
        Color[] _rectangleData = new Color[width * height];
        Color _fillColour;
        if (fill)
        {
            _fillColour = colour;
        }
        else
        {
            _fillColour = Color.Transparent;
        }

        //create top
        for (int x = 0; x < width; x++)
        {
            _rectangleData[x] = colour;
        }
        //create sides
        for (int y = 1; y < height - 1; y++)
        {
            _rectangleData[y * width] = colour;
            for (int x = 1; x < width - 1; x++)
            {
                _rectangleData[y * width + x] = _fillColour;
            }
            _rectangleData[y * width + (width - 1)] = colour;
        }
        //create bottom
        for (int x = 0; x < width; x++)
        {
            _rectangleData[width * (height - 1) + x] = colour;
        }
        Texture2D texture = TextureManager.newTexture2D(width, height);
        texture.SetData(_rectangleData);
        return texture;
    }

Which I call once to make a square which I can use to draw a grid. It is static as it's part of the class I use to handle textures so I'm not passing the graphicsDevice around the whole project. Once I disable this method from being run the error doesn't appear. If it is enabled, it will run for a few seconds before crashing, sometimes the window will turn black. I have already checked to see if it was maybe being looped by something which caused it to overload something, but it is only ever called upon once.

I am using Empty Keys which is a GUI library for Monogame based off WPF, although this doesn't seem to be causing any issues. I am open to changes made in order to draw a square, although I would like to know what's causing this issue so if it comes up in the future I can deal with it.

From your code it's hard to say, I think I need to see a lot more; but that's an error that usually happens from two different functions accessing the same object from your content manager.

For example; on a loading screen, you may have one thread loading sprites and adding them to the content manager, and the other thread drawing the loading sprite. They both access the content manager and collide into that crash.

Consider using locks or better yet: monitor.enter / monitor.exit to protect access to any functions in your TextureManager

monitor.enter can have a timeout time, so you can have it retry for a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM