Could someone help me with this code? When I compile & run my project, it says "Battlefront.exe has stopped working. Windows is looking for a solution to this problem"

This is my code:

//-----------------------------------------------
/// Battlefront.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to provide general functions and the project's entry point
//-----------------------------------------------


#include "Battlefront.h"
#include "PlayerClass.h"
sf::RenderWindow MainWindow(sf::VideoMode(800, 600, 32), "Battlefront INDEV");
GameSection Mode;
PlayerClass PlayerA(UK);


int main()
{
    if(!GameSetUp())
        return EXIT_FAILURE;
    GameLoop();
}

void Draw()
{
    if(Mode == GAME)
    {
        MainWindow.Draw(PlayerA.Arms);
    }
}
//-----------------------------------------------
/// GameEngine.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to provide generic support for actions such as testing for events
//-----------------------------------------------

#include "Battlefront.h"
#include "PlayerClass.h"

bool GameSetUp()
{
    // Declare the global variables
    Mode = GAME;

    if(!MatchSprites())
        return false;
    else
        return true;
}

void GameLoop()
{
    while(MainWindow.IsOpened())
    {
        sf::Event Event;
        while(MainWindow.GetEvent(Event))
        {
            // General "anytime" events
            if(Event.Type == sf::Event::Closed)
                MainWindow.Close();
            // Gamemode specific events
            if(Mode == GAME)
            {
                if(Event.Type == sf::Event::KeyPressed)
                    HandleKeys(Event.Key.Code, true);
                if(Event.Type == sf::Event::KeyReleased)
                    HandleKeys(Event.Key.Code, false);
            }
            else if(Mode == MENU || Mode == OPTIONS || Mode == LOBBY)
            {
                if(Event.Type == sf::Event::MouseButtonPressed)
                    HandleMouseClick(Event.MouseButton.Button, true);
                if(Event.Type == sf::Event::MouseButtonReleased)
                    HandleMouseClick(Event.MouseButton.Button, false);
                if(Event.Type == sf::Event::MouseMoved)
                    HandleMouseMove(Event.MouseMove.X, Event.MouseMove.Y);
            }
        }
        PostEvent();
    }
}

void PostEvent()
{
    MainWindow.Clear();
    Draw();
    MainWindow.Display();
}

void CheckForCollisions()
{
}

bool TestCollision(sf::Sprite Hitter, sf::Sprite Hittee)
{
    return false;
}
//-----------------------------------------------
/// VariableSetUp.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to define all variables with "extern" in Battlefront.h
//-----------------------------------------------

#include "Battlefront.h"
#include "PlayerClass.h"

// Images
sf::Image iUK_Body;
sf::Image iUK_Head;
sf::Image iUK_Arms;

sf::Image iNZ_Body;
sf::Image iNZ_Head;
sf::Image iNZ_Arms;

sf::Image iFeet;

// Others

bool MatchSprites()
{
    if(!iUK_Body.LoadFromFile("Res\\UK_Body.png"))
        return false;
    if(!iUK_Arms.LoadFromFile("Res\\UK_Arms.png"))
        return false;
    if(!iUK_Head.LoadFromFile("Res\\UK_Head.png"))
        return false;
    if(!iNZ_Body.LoadFromFile("Res\\NZ_Body.png"))
        return false;
    if(!iNZ_Arms.LoadFromFile("Res\\NZ_Arms.png"))
        return false;
    if(!iNZ_Head.LoadFromFile("Res\\NZ_Head.png"))
        return false;
    if(!iFeet.LoadFromFile("Res\\Feet.png"))
        return false;

    return true;
}
//-----------------------------------------------
/// HandleInput.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This file's purpose is to provide functions to handle input from events in GameEngine.cpp
//-----------------------------------------------

#include "Battlefront.h"
#include "PlayerClass.h"

void HandleKeys(sf::Key::Code Key, bool IsPressed)
{

}

void HandleMouseClick(sf::Mouse::Button Button, bool IsClicked)
{
    if(Button == sf::Mouse::Left && IsClicked)
        Mode = GAME;
}

void HandleMouseMove(int x, int y)
{
}
//-----------------------------------------------
/// PlayerClass.cpp Source Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// The purpose of this file is to define the class "PlayerClass"
//-----------------------------------------------

#include "Battlefront.h"
#include "PlayerClass.h"


PlayerClass::PlayerClass(Nationality natl)
{
    PlayerFeet.SetImage(iFeet);
    HitPoints = 100;
    CurrentClass = SCOUT_SNIPER;
    if(natl == UK)
    {
        Arms.SetImage(iUK_Arms);
        Body.SetImage(iUK_Body);
        Head.SetImage(iUK_Head);
    }
    else
    {
        Arms.SetImage(iNZ_Arms);
        Body.SetImage(iNZ_Body);
        Head.SetImage(iNZ_Head);
    }
}
PlayerClass::~PlayerClass()
{
}
//-----------------------------------------------
/// Battlefront.h Header Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------

#ifndef BATTLEFRONT_H
#define BATTLEFRONT_H

//----------------------
/// Includes
//----------------------
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

#include <iostream>


//----------------------
/// Enums
//----------------------
enum GameSection { MENU, OPTIONS, LOBBY, GAME };
enum Nationality { US, UK, RU, NZ, JP };
enum GameClass { SUPPORT, MEDIC, ENGINEER, MARKSMAN, OFFICER, SCOUT_SNIPER };

//----------------------
/// Global Variables
//----------------------
extern sf::RenderWindow MainWindow;
extern GameSection Mode;

// Images
extern sf::Image iUK_Body;
extern sf::Image iUK_Head;
extern sf::Image iUK_Arms;

extern sf::Image iNZ_Body;
extern sf::Image iNZ_Head;
extern sf::Image iNZ_Arms;

extern sf::Image iFeet;

// Sprites


//----------------------
/// Function Prototypes
//----------------------
bool GameSetUp();
void GameLoop();
void PostEvent();
bool TestCollision(sf::Sprite, sf::Sprite);
void CheckForCollisions();

void Draw();
bool MatchSprites();

void HandleKeys(sf::Key::Code, bool IsPressed);
void HandleMouseClick(sf::Mouse::Button, bool IsClicked);
void HandleMouseMove(int x, int y);


#endif
//-----------------------------------------------
/// PlayerClass.h Header Code File
/// Part of the Battlefront.cbp Code::Blocks Project
//-----------------------------------------------
/// This is a class header file for the class "PlayerClass"
//-----------------------------------------------

#ifndef PLAYERCLASS_H
#define PLAYERCLASS_H

#include "Battlefront.h"

class PlayerClass
{
    public:
    PlayerClass(Nationality);   // Constructor
    ~PlayerClass();             // Destructor
    sf::Sprite PlayerFeet;
    sf::Sprite Arms;
    sf::Sprite Body;
    sf::Sprite Head;
    int HitPoints;
    GameClass CurrentClass;
};

#endif

The only idea I could come up with as to why the .exe file stops working is that maybe I'm using MainWindow incorrectly?
By the way, if you couldn't tell, I'm using SFML, hence the sf namespace.

Recommended Answers

All 10 Replies

OK, I took the time to download SFML (I was curious about it, anyway, so it wasn't a big deal), and compiled and tested your program. After setting some breakpoints at various places, I found that it is raising a SIGSEGV (that is, a segmentation violation) in the initialization of PlayerA, on the line

PlayerFeet.SetImage(iFeet);

The call stack at the point where it segfaults reads like so:

#0 00A18C6B std::_Rb_tree_decrement(__x=0x406044) (../../../../gcc-4.4.0/libstdc++-v3/src/tree.cc:89)
#1 00A02CAF std::_Rb_tree_iterator<sf::ResourcePtr<sf::Image>*>::operator--(this=0x28fd48) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_tree.h:199)
#2 00A10E3E std::_Rb_tree<sf::ResourcePtr<sf::Image>*, sf::ResourcePtr<sf::Image>*, std::_Identity<sf::ResourcePtr<sf::Image>*>, std::less<sf::ResourcePtr<sf::Image>*>, std::allocator<sf::ResourcePtr<sf::Image>*> >::_M_insert_unique(this=0x406040, __v=@0x28fdfc) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_tree.h:1179)
#3 00A09B48 std::set<sf::ResourcePtr<sf::Image>*, std::less<sf::ResourcePtr<sf::Image>*>, std::allocator<sf::ResourcePtr<sf::Image>*> >::insert(this=0x406040, __x=@0x28fdfc) (d:/programmes/mingw-4.4/bin/../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_set.h:411)
#4 009C83A6 sf::Resource<sf::Image>::Connect(this=0x406040, Observer=...) (../../include/SFML/System/Resource.inl:77)
#5 009C4678 sf::ResourcePtr<sf::Image>::operator=(this=0x4062cc, Resource=0x406040) (../../include/SFML/System/ResourcePtr.inl:102)
#6 00922CB4 sf::Sprite::SetImage(this=0x406220, Img=...) (D:\dev\sfml\sdk\SFML-1.6\src\SFML\Graphics\Sprite.cpp:72)
#7 004016CA PlayerClass::PlayerClass(this=0x406220, natl=UK) (C:\Users\Schol-R-LEA\Documents\Programming\Projects\Quick Tests\Battlefront\PlayerClass.cpp:15)
[color=red]#8 00402D16  __static_initialization_and_destruction_0(__initialize_p=<optimized out>, __priority=<optimized out>) (C:\Users\Schol-R-LEA\Documents\Programming\Projects\Quick Tests\Battlefront\Battlefront.cpp:13)[/color]
#9 00000000 _GLOBAL__sub_I_MainWindow() (C:\Users\Schol-R-LEA\Documents\Programming\Projects\Quick Tests\Battlefront\Battlefront.cpp:29)
#10 0040244F    __do_global_ctors() (../mingw/gccmain.c:59)
#11 00401098    __mingw_CRTStartup() (../mingw/crt1.c:236)
#12 00401284    mainCRTStartup() (../mingw/crt1.c:264)

The most relevant part of the call stack is highlighted in red; it shows that the code where it is failing is being called in the static initialization, which takes place before main() begins.

Now, the image files are supposed to get loaded in MatchSprites(); however, because you are initializing PlayerA as a global variable, the c'tor is actually being called before MatchSprites() has run (and in fact before the beginning of main(), with the result that iFoot hasn't been initialized at the time when you are setting the the image as part of the PlayerA object, with the result that an invalid pointer is being passed to _Rb_tree_decrement() (don't worry about what that is, it is a standard library call that the SFML libraries are using).

The best way to avoid this is to move the declaration of PlayerA out of the global namespace, or at the very least, not actually initialize it until after MatchSprites() has run. Unfortunately, this means changing how you are accessing the PlayerA object in other parts of the code; on the other hand, it should improve the generality of your code, and will reduce the scope that PlayerA is visible through, which is all to the good.

BTW, could you zip up a copy of the sprites in question, so I could follow along through the whole process? As it is, even if you got the program running on your system, we wouldn't be able to help you debug it as we wouldn't have the sprite files.

Ok, so now I added arguments to GameLoop() , PostEvent() and Draw() , all of which are of type PlayerClass . Thus, I changed Main() to:

int main()
{
    if(!GameSetUp())
        return EXIT_FAILURE;
    PlayerClass PlayerA(UK);
    GameLoop(PlayerA);
}

( GameSetUp() calls MatchSprites() , which thus means PlayerA is delcared after MatchSprites() .) GameLoop() then passes PlayerA to PostEvent() , which then passes it to Draw() .
To allow the function prototypes to still be up to date, I #included PlayerClass.h right before the function prototypes section in Battlefront.h.
This is all I have changed, and it appears the program still stops working.

One last thing, by "sprites" do you mean the image files, like UK_Body.png, etc.?

One last thing, by "sprites" do you mean the image files, like UK_Body.png, etc.?

Yes.

All the images are stored in the Res.zip folder I've attached.

Thank you, those help quite a bit.

Did you have any problem compiling with those changes? I had to move the Nationality and GameClass into PlayerClass.h and remove the cyclical #include "battlefront.h" from PlayerClass.h as well; once I did that, I got it to run, at least to the point that an image comes up on the screen.

Hm. Strange. I did the same:

#ifndef PLAYERCLASS_H
#define PLAYERCLASS_H


enum Nationality { US, UK, RU, NZ, JP };
enum GameClass { SUPPORT, MEDIC, ENGINEER, MARKSMAN, OFFICER, SCOUT_SNIPER };

class PlayerClass
{
    public:
    PlayerClass(Nationality);   // Constructor
    ~PlayerClass();             // Destructor
    sf::Sprite PlayerFeet;
    sf::Sprite Arms;
    sf::Sprite Body;
    sf::Sprite Head;
    int HitPoints;
    GameClass CurrentClass;
};

#endif

And it still doesn't run correctly.

Which compiler/IDE are you using? I've been testing using Code::Blocks and the latest version of MinGW GCC (4.6.2) under Windows 7, but if your testing in, say, Visual Studio, or GCC for some other operating system, you may be getting different results.

I'm using Code::Blocks ver. 10.05 (not sure about MinGW) and Windows 7 as well.

Could you post the project in a zip file so I can download it, to make sure it's a compiler issue and I didn't just write a typo?

Could you post the project in a zip file so I can download it, to make sure it's a compiler issue and I didn't just write a typo?

Certainly.

I think it's a problem with my MinGW. I had to download libstdc++-6.dll , and when I ran the program it said: The application was unable to start correctly (0xc0150002). Click OK to close the application. How do I tell what version of MinGW I have, and how do I update it if needed?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.