简体   繁体   中英

C++/SFML program crashes on exit

I just recently started playing around with SFML and I wrote this simple program.

I'm using visual studio 2010 btw.

The program compiles and runs fine when using the "start debugging" option. but if I open the .exe file as if I was running a normal desktop application or something, it will crash on exit.

I've spent a while trying to figure it out but all I can say is that it's probably a heap corruption.

here's all the code:

#include <iostream>
#include <sstream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


void moveSquare();
void avgFPS();

class displayFPS : public sf::Thread{
public:
private:
    virtual void Run();
};

int checkEvent(sf::RenderWindow &win){
    sf::Event Event;
    while(win.GetEvent(Event)){
        // Window closed
        if (Event.Type == sf::Event::Closed){
            return 0;
        }
        // Escape key pressed
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)){
            return 0;
        }
    }
    return -1;
}

sf::RenderWindow win(sf::VideoMode(800,600,32),"Mario Clone Test");
sf::Image img1(200,200,sf::Color(255,255,0));
sf::Sprite sprite1;
std::stringstream ss;
sf::String fps;
bool threadFPS;

int main(){
    sprite1.SetImage(img1);
    sprite1.SetCenter(-300,-300);
    win.SetFramerateLimit(30);
    moveSquare();
    win.Close();
    sf::Sleep(0.5);
    return 0;
}

void moveSquare(){
    displayFPS dispFPS;
    threadFPS = true;
    dispFPS.Launch();
    fps.SetSize(20);
    while(1){
        if(!win.IsOpened() || checkEvent(win) == 0){
            threadFPS = false;
            dispFPS.Wait();
            break;
        }
        win.Draw(sprite1);
        win.Draw(fps);
        win.Display();
        win.Clear();
        if(win.GetInput().IsKeyDown(sf::Key::Left)){
            sprite1.Move(-100*win.GetFrameTime(),0);
        }
        if(win.GetInput().IsKeyDown(sf::Key::Right)){
            sprite1.Move(100*win.GetFrameTime(),0);
        }
        if(win.GetInput().IsKeyDown(sf::Key::Up)){
            sprite1.Move(0,-100*win.GetFrameTime());
        }
        if(win.GetInput().IsKeyDown(sf::Key::Down)){
            sprite1.Move(0,100*win.GetFrameTime());
        }
    }
    return;
}

void avgFPS(){
    double frames=0.0,avg=0.0;
    int j=0;
    while(threadFPS){
        if(win.GetFrameTime() != 0){
            j++;
            frames = frames+(1.0/win.GetFrameTime());
            avg = frames/j;
        }
        ss << "avg FPS: " << avg << std::endl << "Arrow Keys to Move" << std::endl << "Press ESC to Exit";
        fps.SetText(ss.str());
        ss.str("");
    }
    return;
}

void displayFPS::Run(){
    avgFPS();
}

I've had the same issue. You need to recompile SFML when using VS2010.

SFML has multiple versions of their .lib's for release and debug.

Examples:

  • sfml-audio.lib
  • sfml-audio-d.lib
  • sfml-audio-s.lib
  • sfml-audio-sd.lib

Make sure you are using the lib without the -d in it.

Also, when you put the .dll's with your exe (assuming you are using the dynamic libraries) make sure to use the normal versions not the debug (-d) versions.

Finally, when you are building the project make sure you build for release and not debug.

Few things for you to try:

  1. If you are suspecting heap corruption, run gflags (found in Debugging Tools for Windows ) and enable page heap. Some instructions on how it works can be found here . Basically when page heap is enabled, your app will crash at the point of the memory error, not sometime later.
  2. You said you get a crash on exit. When that happens, I'm assuming windows throws up a crash dialog box. Open one of those links that say something like "see what information is being uploaded". Somewhere among those files will be a minidump of your process. You can load that up in visual studio (open file and hit F5). Sometimes visual studio is glitchy, so another, more reliable but more difficult but more difficult to use alternative is WinDbg, also part of Debugging Tools for Windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM