C++/SFML program crashes on exit - c++

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.

Few things for you to try:
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.
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.

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-s-d.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.

Related

Visual Studio doesn't want me to code or something (Deleting debug)

So I was checking the files for my C++ game, and I see the .exe file already created. I coded a bit to see if it works, and when I debugged it, it said the .exe file was not found. And indeed it was not there. Did I do something wrong? Here is the code
#include <iostream>
#include <SFMl\Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(320, 480), "The Game!");
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
window.clear(Color::White);
window.display();
}
return 0;
}
I tested your snippet and it worked fine.
Try to turn off your antivirus software, or test it on other devices.

Problem with SMFL Syntax in C++ Visual Studio importing png

-I am new to C++ and SFML and want to import a png file.
-It worked a view times, but afterwards I got the
message "Build failed, run last success?" most of the time. Sometimes its still working.
-there is no "real" error, so its hard to figure out, what the problem is
-I read earlier, that switching from Debug to Release-Mode could be a reason, but it didnt helped
its working when I dont use:
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
-> but, ofcourse, the sprite is missing then.
I would be happy to have a solution/reason for this or a topic I missed so far to read/ learn about.
Im happy about advices.
Thanks so far.
Alex
VISUAL STUDIO 2019
x64
SFML-2.5.1
WHOLE CODE:
#include"SFML/Graphics.hpp"
#include<iostream>
#include"main.h"
int main(int argc, char** argv[])
{
sf::RenderWindow window(sf::VideoMode(1200,800), "bimWindow");
sf::RectangleShape rs(sf::Vector2f(1000, 700));
rs.setFillColor(sf::Color::Green);
sf::Event event;
sf::Texture texture;
if (!texture.loadFromFile("assets/player.png"))
{
std::cout << "Could not load png \n";
return 0;
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(100,100);
//sprite.scale(sf::Vector2f(3, 3));
rs.setPosition(80, 80);
// run the program as long as the window is open:
while (window.isOpen())
{
//let window open i guess.
while (window.pollEvent(event)); //stay true as long aas it didnt happen or so
{
// "close requested" event:close the window
if(event.type == sf::Event::Closed)
window.close();
}
//RENDER:
window.clear();
window.draw(rs);
window.draw(sprite);
window.display();
}
return 0;
}
Try making sure that the "assets/player.png" file is inside your solution directory (the folder with the .sln file.) Your code seems to run fine on my end, and that is the only error I can think of. If you can, it would help to have the error message(s) that you are receiving. If the png file is in the right place and you still get errors, I would recommend reinstalling SFML and following a tutorial online to ensure that you get everything set up properly.

Visual Studio(C++/SDL2) nvd3dum.pdb could not be found in the selected paths

Recently I had alot of graphic driver issues, so today I used Display Driver Uninstaller to remove my Nvidia drivers and do a clean install. All good, but now when I try to compile one of my programs I get the following error:
"nvd3dum.pdb could not be found in the selected paths" (exception thrown)
What I tried so far:
reinstall the Nvidia drivers(again), since its a Nvidia file that should be the problem?
repair visual studio and update
download all symbols via windows server (Tools->Debugging->Symbols->Server)
I use the boost lib, SDL2(+image,audio,ttf,net),lua/c++ binding, its an "older" project created with VS2015
I tried another program(also SDL2), I get the same warning but it doesnt throw any exception and works fine.
Apparently the error is caused by a second thread:
LoadingScreen.start();
Stuff.Load(); //load SDLTextures
LoadingScreen.end();
//-----------
class LoadingScreen
{
public:
LoadingScreen();
void start() {
mIsActive = true;
mThread = std::thread(&LoadingScreen::render, this);
std::cout << "Start: Loading Screen" << std::endl;
}
void end() {
mIsActive = false;
mThread.join();
std::cout << "End: Loading Screen" << std::endl;
}
private:
void render()
{
while (mIsActive)
{
SDL_RenderClear(gRenderer);
mLoading.render(NULL,angle);
SDL_RenderPresent(gRenderer);
angle = angle + 1.25;
}
}
int mPercent;
std::thread mThread;
bool mIsActive = true;
DefaultTexture mLoading;
};

SFML - RenderWindow Runtime Errors with EVERYTHING

For almost all of any object I've added so far (sf::Color, sf::Image, sf::Texture, etc.) to the sample code I grabbed from the SFML site, exceptions get thrown once I run it, to the accord of:
Unhandled exception at 0x61C71B86 (sfml-system-2.dll) in gameboiss.exe: 0xC0000005: Access violation reading location 0x00000074.
It's brought up in reference to the RenderWindow line for whatever reason. If I remove all lines referencing these objects, it works fines. Even if I leave only one line creating the variable (i.e. sf::Texture texture), the exception is still thrown. I've tried multiple things amongst looking at the locals/autos at debug and it doesn't seem to give much light.
I'm working on Visual Studio 2012, any help would be appreiciated. Below is the code. thanks :)
#include <iostream>
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "SFML/System.hpp"
int main(){
sf::RenderWindow window(sf::VideoMode(200,200), "game boi");
sf::CircleShape shape(100.f);
//source of error
sf::Texture texture;
texture.loadFromFile("char\\spartapix.png");
/*if(!hero.loadFromFile("char\\spartapix.png")){
std::cerr << "Error: sprite not loaded.\n";
return 1;
}*/
//sf::Image background;
//if (!background.loadFromFile("background.jpg"))
//return -1;
shape.setFillColor(sf::Color::Blue);
sf::Vector2i pos;
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::GainedFocus)
std::cout << "playing";
if (event.type == sf::Event::LostFocus)
std::cout << "notplaying\n";
if (event.type == sf::Event::MouseButtonPressed){
sf::Vector2i pos = sf::Mouse::getPosition(window);
std::cout << pos.x << " " << pos.y << std::endl;
}
if (event.type == sf::Event::KeyPressed){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
}
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
<code>
I agree with RetiredNinja's comment above: If it happens as soon as you create the render window, it's most likely some STL version difference (from the passed string; since sf::String is part of sfml-system), which is typically the case when you mix up versions (Visual Studio releases or Release/Debug builds).
Are you sure you've picked the correct download from SFML's download page? As an alternative, you could try downloading and compiling SFML from the official repository.

A simple SDL application does not work with x64 configuration using Visual Studio 2012

I coded a simple application using SDL which displays a simple window on Windows 8 (64 bits). In a first time I compiled and executed my code with Win32 configuration (default configuration) and the program works perfectly. Now I want to have the same execution but this time with x64 configuration. So I configured Visual using 'configurations manager' in my project properties and change my SDL.lib and SDLmain.lib choosing x64 libraries in the linker. The project compilation is ok but the execution fails saying that the application has failed to start properly. Here's a screen of the message (the memory address is always the same at each execution) :
And my c++ code :
#include <iostream>
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <GL/glu.h>
#define WIDTH 500
#define HEIGHT 500
static float angle = 0.0f;
static void eventListener(SDL_Event *pEvent, bool *pContinue)
{
while (SDL_PollEvent(pEvent))
{
switch(pEvent->type)
{
case SDL_QUIT:
*pContinue = false;
break;
case SDL_KEYDOWN:
switch (pEvent->key.keysym.sym)
{
case SDLK_ESCAPE:
*pContinue = false;
break;
}
break;
}
}
}
#undef main
int main(void)
{
SDL_Event myEvent;
bool isAlive = true;
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Simple SDL window", NULL);
SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL);
while (isAlive == true)
{
eventListener(&myEvent, &isAlive);
}
SDL_Quit();
return (0);
}
I don't understand this message which is not precise. However my x64 SDL libraries linked to my project seems to be correct because the compilation is ok. So I wonder what's happening here. Does anyone already have encountered the same problem ?
Just googled for your error message, and it says that this error code (0x0c000007b) means INVALID_IMAGE_FORMAT.
This means that either you are mixing 32 and 64 bit binaries or you have corrupted binaries. Try to place you binary and your dependencies at the same folder and run the application. If the error continues, than one of your libraries must be corrupted. Else, it was a problem with the Windows loading a library for a different platform of your compiled binary.