Memory access error when using TGUI Picture and sfml primitives

Started by noxabellus, 06 November 2017, 23:19:54

noxabellus

If I write the following code

RenderWindow window = new RenderWindow(new VideoMode(400, 300), "TGUI.Net test");
Gui gui = new Gui(window);

Picture picture = new Picture("background.jpg");
gui.Add(picture);

// ... gui.Add various test elements ...

Vertex[] test =  {
                new Vertex(new Vector2f(), Color.White),
                new Vertex(new Vector2f(400, 0), Color.Magenta),
                new Vertex(new Vector2f(400, 75), Color.Cyan),
                new Vertex(new Vector2f(0, 75), Color.Yellow)
            };

while (window.IsOpen) {
    window.DispatchEvents();

    window.Clear();

    window.Draw(test, PrimitiveType.Quads);

    gui.Draw(); // crash, Exception Unhandled

    window.Display();
}



I get a
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'



But if I comment out gui.Add(picture) it works fine and all other tgui elements and my sfml primitives draw correctly....

Alternatively if I comment out window.Draw(test, PrimitiveType.Quads); it works as well

Or if I change the order
gui.Draw();
window.Draw(test, PrimitiveType.Quads);


it works but of course my "game content" draws on top of my gui

I also notice that standard builtin sfml types work, such as Sprite. It seems the problem arises only when used in conjunction with this window.Draw(Vertex[], PrimitiveType) signature, but I also tried abstracting this into a class that can be called with window.Draw(instance)
class MyTestDrawable : Transformable, Drawable {
        private Vertex[] _Vertices;

        public MyTestDrawable() {
            _Vertices = new Vertex[] {
                new Vertex(new Vector2f(), Color.White),
                new Vertex(new Vector2f(400, 0), Color.Magenta),
                new Vertex(new Vector2f(400, 75), Color.Cyan),
                new Vertex(new Vector2f(0, 75), Color.Yellow)
            };
        }

        public void Draw(RenderTarget target, RenderStates states) {
            states.Transform *= Transform;
            target.Draw(_Vertices, PrimitiveType.Quads, states);
        }
    }
to no avail

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Further testing has revealed that if I first draw a standard sfml type like Sprite, I can then draw with (Vertex[], PrimitiveType)
window.Draw(spriteInstance);

window.Draw(test, PrimitiveType.Quads);

gui.Draw();

with no errors.

texus

I'll try to look into this tonight. Thanks for the detailed description.
Just in case I wouldn't be able to reproduce it: what compiler are you using?


texus

After spending some time debugging this, it seems to be an issue with SFML. Luckily they made some changes in the related code 4 weeks ago (while the SFML version shipped with TGUI.Net was 5 weeks old) which seem to have fixed the problem. I have updated the SFML version now, if you download TGUI.Net again and use the new files then the crash should be fixed.