If I write the following code
Code: [Select]
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
Code: [Select]
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'


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

Alternatively if I comment out
Code: [Select]
window.Draw(test, PrimitiveType.Quads); it works as well

Or if I change the order
Code: [Select]
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)
Code: [Select]
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)
Code: [Select]
window.Draw(spriteInstance);

window.Draw(test, PrimitiveType.Quads);

gui.Draw();
with no errors.
« Last Edit: 07 November 2017, 00:36:34 by noxabellus »

*

texus

  • *****
  • 1280
    • View Profile
    • Texus's Blog
Re: Memory access error when using TGUI Picture and sfml primitives
« Reply #1 on: 07 November 2017, 08:05:07 »
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?

Re: Memory access error when using TGUI Picture and sfml primitives
« Reply #2 on: 07 November 2017, 17:56:38 »
Im using .net core 2 + VS 2017

*

texus

  • *****
  • 1280
    • View Profile
    • Texus's Blog
Re: Memory access error when using TGUI Picture and sfml primitives
« Reply #3 on: 07 November 2017, 20:29:06 »
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.

Re: Memory access error when using TGUI Picture and sfml primitives
« Reply #4 on: 07 November 2017, 21:15:50 »
Awesome, thanks!