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

Author Topic: AccessViolationException when changing properties  (Read 5790 times)

0 Members and 1 Guest are viewing this topic.

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« on: July 17, 2009, 01:36:44 am »
hi there,
ive got a silly problem.

im writing a textbox right now and ive encountered several errors with String2d.

when i change and read properties like Text several times before drawing (often initiated through events) i get the AccessViolationException.

Code: [Select]
System.AccessViolationException was unhandled
  Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
  Source=sfmlnet-graphics
  StackTrace:
       at SFML.Graphics.String2D.sfString_GetText(IntPtr This)
       at SFML.Graphics.String2D.get_Text()
  [...]


am i doing something wrong or is this a bug? how can i avoid this?

(the last days i avoided this by changing text just before drawing, but now i need to change it several times as i try to measure the width of the string)

this is the code:

Code: [Select]
       public int Width
        {
            get
            {
                string oldText = Text;
                if (oldText.Length == 0)
                    return 0;

                float start = drawable.GetCharacterPos(0).X;
                Text = oldText + "|";
                float end = drawable.GetCharacterPos((uint)oldText.Length).X;

                Text = oldText;
                return (int)(end - start);
            }
        }

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
AccessViolationException when changing properties
« Reply #1 on: July 17, 2009, 08:40:31 am »
Can you extract a tiny and complete source code that reproduces the problem?
Laurent Gomila - SFML developer

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« Reply #2 on: July 17, 2009, 10:17:00 am »
i hope this works for you (i added a fps counter to see when it crashes)

it has to do with changing the text within events.
the events are not running in seperate threads arent they?

Code: [Select]
using System;

namespace SFMLTest
{
    public class Blub
    {
        SFML.Graphics.RenderWindow window;
        SFML.Graphics.String2D stringObject;
        SFML.Graphics.String2D fps;
        //string content = "";
        public void Run()
        {
            window = new SFML.Graphics.RenderWindow(new SFML.Window.VideoMode(800, 600), "test");
            window.Closed += new EventHandler(OnClose);
            window.TextEntered += new EventHandler<SFML.Window.TextEventArgs>(OnTextEntered);
            window.SetFramerateLimit(60);

            stringObject = new SFML.Graphics.String2D("text");
            stringObject.Position = new SFML.Graphics.Vector2(10, 100);
            fps = new SFML.Graphics.String2D("0");
            fps.Position = new SFML.Graphics.Vector2(500, 10);

            while (window.IsOpened())
            {
                float deltaTime = window.GetFrameTime();

                window.DispatchEvents();

                Update();

                window.Clear();
                Draw();
                window.Display();
            }
        }

        private void Update()
        {
            fps.Text = "" + (1f / window.GetFrameTime());
            //stringObject.Text = content;
        }

        private void Draw()
        {
            window.Draw(stringObject);
            window.Draw(fps);
        }

        private void OnTextEntered(object sender, SFML.Window.TextEventArgs args)
        {
            if (args.Unicode.ToCharArray()[0] > 31)
            {
                //content += args.Unicode;
                stringObject.Text += args.Unicode;
            }
        }

        private void OnClose(object sender, EventArgs e)
        {
            window.Close();
        }
    }
}


it breaks in OnTextEntered when typing very fast.

when im using a seperate string to modify and then assign it in update to the string2d (commented out), it works just fine.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
AccessViolationException when changing properties
« Reply #3 on: July 17, 2009, 10:25:48 am »
Quote
the events are not running in seperate threads arent they?

No, everything is done in the main thread.

I tried your example and it works fine for me, I couldn't reproduce the crash.
Laurent Gomila - SFML developer

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« Reply #4 on: July 17, 2009, 10:51:15 am »
im using windows 7 and visual studio 2010.

i tried to build on;

.Net 2.0 - CRASH
.Net 3.0 - CRASH
.Net 3.5 - CRASH
.Net 4.0 - works fine
mono 2.4 - CRASH (this is what i work on)

i will try on my laptop with winXP if it behaves differently

////
on winxp it works fine (just crashes when closing, but thats a different problem). i guess this is a windows 7 - .net framework related problem

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« Reply #5 on: July 29, 2009, 01:25:48 pm »
hi
Code: [Select]
A first chance exception of type 'System.AccessViolationException' occurred in sfmlnet-graphics.dll

it comes out of your dll so maybe its not compatible with win7... i dont know. if you have some time and access to windows 7 you might try to reproduce the error.

for now i catch the exception and it works but its kinda strange.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
AccessViolationException when changing properties
« Reply #6 on: July 29, 2009, 02:17:03 pm »
Have you tried recompiling everything (SFML, CSFML, SFML.Net) on Windows 7?
Laurent Gomila - SFML developer

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« Reply #7 on: July 29, 2009, 02:36:29 pm »
thats a good idea, i will try later

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« Reply #8 on: July 29, 2009, 04:04:04 pm »
embedded SFML.net, sadly that did not help. it breaks @

Code: [Select]
           public string Text
            {
                // TODO : use unicode functions
                // (convert from UTF-16 to UTF-32, and find how to marshal System.String as sfUint32*...)
                get {return sfString_GetText(This);} // <<<< here
                set {sfString_SetText(This, value);}
            }


i will try to compile the rest

thoniel

  • Newbie
  • *
  • Posts: 21
    • ICQ Messenger - 157157736
    • View Profile
AccessViolationException when changing properties
« Reply #9 on: July 29, 2009, 05:37:47 pm »
sorry for my 3rd post but that didnt help.

- i downloaded the trunk
- compiled sfml
- compiled csfml
- imported the .net projects of sfml into my project and replaced csfml* with my newly build files.

then i deleted every build of my project
end rebuild, also putting the new csfml files into the build folder.

sadly, the problem remains.

btw. what is cfsml for anyway? why dont you use your c++ lib directly?
sorry if thats a dump question, i have very small knowledge about c/c++

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
AccessViolationException when changing properties
« Reply #10 on: July 29, 2009, 05:48:22 pm »
dotnet can use C++ only through C++/CLI, which is not supported on Linux (Mono) yet.
C binaries are the only standard thing that can be used by a dotnet program.
Laurent Gomila - SFML developer

 

anything