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

Author Topic: Program Crashes when quitting  (Read 2917 times)

0 Members and 1 Guest are viewing this topic.

Nasaman

  • Guest
Program Crashes when quitting
« on: November 09, 2017, 01:10:19 pm »
Hey everyone!

I've been working on a game and whenever I close the SFML Window, a.k.a. when the game loop ends, the window closes but the console windows stays and I get a "the program is not responding" error from windows.
By manually attaching a debugger I found this only error message:
Quote
Unhandled exception at 0x777122D2 (ntdll.dll)

After googling a bit I found out that it may have to do with my RenderWindow being static, I changed that but it still crashes.

Here is the relevant code (Main):
using System;


namespace GameProject
{
#if WINDOWS || LINUX

    class Program
    {
        public static SFML.System.Clock gameClock;

        [STAThread]
        static void Main() {
            StateManager Game = StateManager.Instance; //The instance is created here.
            Game.Initialize();
            Game.LoadContent();
            gameClock = new SFML.System.Clock();
            float gameTime;
            while(!StateManager.Ended) { //When the close button is clicked, this becomes true
                gameTime = gameClock.ElapsedTime.AsMicroseconds()/1000f;
                gameClock.Restart();
                Game.Update(gameTime);
                Game.Draw();
            }
            Game.UnloadContent();
            gameClock.Dispose();
            Environment.Exit(0);
        }
    }

#endif
}

The Constructor of the StateManager looks like this:
        public void Initialize() {
            Window = new RenderWindow(new VideoMode(1280, 720), "Project-F", Styles.Close);
            Window.SetVerticalSyncEnabled(false);
            Window.GainedFocus += new EventHandler(OnGainedFocus);
            Window.LostFocus += new EventHandler(OnLostFocus);
            Window.Closed += new EventHandler(OnClosed);
            Window.SetVisible(true);
        }

This just sets Ended to true
        private void OnClosed(object sender, EventArgs e) {
            Ended = true;
        }

And this is the UnloadContent method of the StateManager:
        public void UnloadContent() {
            _currentState.UnloadContent();
            Window.Close();
            Window.Dispose();
        }
_currentState.UnloadContent(); just disposes a few textures.


Additionally I could track the crash down to in between these two lines:
            gameClock.Dispose();
            Environment.Exit(0);

Any idea why that happens?
Thank you in advance.

PS: Here the full code of StateManager.cs if needed: https://pastebin.com/sa0apq0V

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: Program Crashes when quitting
« Reply #1 on: November 09, 2017, 01:32:57 pm »
What's the call stack?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nasaman

  • Guest
Re: Program Crashes when quitting
« Reply #2 on: November 09, 2017, 02:21:10 pm »
Edit: This is the Call Stack and Locals at Environment.Exit(0):
https://imgur.com/a/FWkZv
« Last Edit: November 09, 2017, 02:25:07 pm by Nasaman »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: Program Crashes when quitting
« Reply #3 on: November 09, 2017, 02:29:58 pm »
Sounds like something is trying to access something that has already been deleted. Since you use Environment.Exit(0), do you use threads?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nasaman

  • Guest
Re: Program Crashes when quitting
« Reply #4 on: November 09, 2017, 03:21:59 pm »
I do not, only thing I could think of if the problem might be Thread related is that the EventHandlers might not be disposed of properly(?)

But yeah, that AccessViolation is very weird.

Any ideas?

Nasaman

  • Guest
Re: Program Crashes when quitting
« Reply #5 on: November 09, 2017, 03:45:12 pm »
Could it maybe be that SFML uses some OpenGL functions higher than 2.1 together with the Disposal/Ending process?

Nasaman

  • Guest
Re: Program Crashes when quitting
« Reply #6 on: November 10, 2017, 12:52:54 am »
Alright, I've tried it out on my PC with a High-End GPU (OpenGL 4.5 I believe) and it works just fine there.
It seems like the problem is caused by my laptop only having OpenGL 2.1 support.

Any ideas for a possible workaround/fix?
or even why that is a thing?

 

anything