Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: System.AccessViolation  (Read 27276 times)

0 Members and 2 Guests are viewing this topic.

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
System.AccessViolation
« on: July 16, 2013, 11:39:50 am »
After creating a RenderWindow and rendering lots of pretty things, i randomly had this piece of code throwing AccessViolations from the Text constructor.

public int GetTextWidthPixels(string text, Font font, uint fontSize)
{
       var textShape = new Text(text, font, fontSize);
       var bounds = textShape.FindCharacterPos((uint)(text.Length));
       textShape.Dispose();
       return (int)bounds.X;
}

The font is a static loaded resource, but to make sure, I've tested that the constructor doesn't misbehave if I pass null or a disposed Font, so this is not the issue.

I'm wondering if you could share any insight into the things that could be causing this behavior, so i know what to look for.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #1 on: July 16, 2013, 11:43:17 am »
Don't you have more detailed information about the exception? Like the call stack, or a human-friendly message?
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #2 on: July 16, 2013, 11:48:27 am »
Well, the nature of AccessViolation is not very informative, which is why I'm asking :)

This is the top of the stack:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  at SFML.Graphics.Text.sfText_create()
  at SFML.Graphics.Text..ctor(String str, Font font, UInt32 characterSize)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #3 on: July 16, 2013, 11:58:21 am »
Make sure that the CSFML DLLs match your version of SFML.Net (do you have several version, or did you update an older version of SFML.Net?).
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #4 on: July 16, 2013, 12:07:08 pm »
I've just verified that my dlls are the ones that go together out of the 32 bit .Net binary package.

FYI: Don't know if it helps, but I also experienced AccessViolations from the sprite copy constructor. I didn't mention this before because I haven't ruled out that i could be passing disposed sprites yet (which would also cause an AccessViolation).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #5 on: July 16, 2013, 12:52:17 pm »
Is it only these two calls that fail, or does any call to a sfml-graphics function result in the same thing?
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #6 on: July 17, 2013, 12:02:47 pm »
I also have reports for
 
at SFML.Graphics.Sprite.sfRenderWindow_drawSprite(IntPtr CPointer, IntPtr Sprite, MarshalData& states)
at SFML.Graphics.Sprite.Draw(RenderTarget target, RenderStates states)
 
and also at
 
at SFML.Graphics.Text.sfRenderWindow_drawText(IntPtr CPointer, IntPtr Text, MarshalData& states)
at SFML.Graphics.Text.Draw(RenderTarget target, RenderStates states)
 
and at
 
at SFML.Graphics.Shape.sfShape_create(GetPointCountCallbackType getPointCount, GetPointCallbackType getPoint, IntPtr userData)
at SFML.Graphics.Shape..ctor()
 
So, pretty much all around. It usually occurs after a few hours of rendering. Profiling doesn't show any mem leaks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #7 on: July 17, 2013, 02:23:05 pm »
Quote
It usually occurs after a few hours of rendering
Ah. So these functions work, but after a few hours the binding somehow gets unstable, right?
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #8 on: July 19, 2013, 01:29:52 am »
Correctly understood.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #9 on: July 19, 2013, 07:57:16 am »
Probably a memory issue. Does the memory usage of your app increase over time?
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #10 on: July 19, 2013, 10:12:27 am »
Which is what i guessed as well, but the leakeage is minor, if any, after profiling with ants.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #11 on: July 19, 2013, 10:34:17 am »
Since SFML.Net invokes CSFML dynamically, the profiler might not see everything that happens there.
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #12 on: July 21, 2013, 11:43:42 pm »
Hey Laurent.
I can't seem to find any information on sfml memory management in .Net, so I'm wondering how I am supposed to go about this. At first I assumed it was handled for me. But since the Texture destructor makes a Destroy(false) call, this causes a leak. I guess it does this because you don't want to release resources on the GC thread. Is it right to conclude I MUST call Texture.Dispose() on my main thread before loosing the Texture reference to avoid any leaks?
Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: System.AccessViolation
« Reply #13 on: July 22, 2013, 08:04:20 am »
No it's wrong ;)

SFML resources are always destroyed (look at what Destroy(false) does). When it's done in the GC thread I just instanciate an OpenGL context because there's none.
Laurent Gomila - SFML developer

myl

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • Email
Re: System.AccessViolation
« Reply #14 on: July 22, 2013, 08:46:58 am »
Glad I asked then :) This minimal example is leaking full on. What am I misunderstanding?
var renderWindow = new RenderWindow(new SFML.Window.VideoMode(200, 200), "I'm a leaker");
while (true)
{
    var texture = new Texture("nonprogressive.jpg");
    var sprite = new Sprite(texture);
    renderWindow.Draw(sprite);
    renderWindow.Display();
    renderWindow.DispatchEvents();
    GC.Collect();
}
 
« Last Edit: July 22, 2013, 08:54:05 am by myl »