Crashing program because of one class

I am developing my first game with SFML 2.1 and am trying to do it efficiently but when I run it I get an error status of 255, in other words it's crashing. It compiled successfully but wont run successfully. I scraped down pieces of code until it would stop crashing and figured out it was one class causing it.
That class is "Screen.h/.cpp" but I'm going to show you all the code of the classes with actual code in it.

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "Screen.h"
#include "Screen2.h"
#include "MainMenu.h"
#include "Splashscreen.h"
#include <SFML/Graphics.hpp>




int main() {


sf::RenderWindow Gamewindow(sf::VideoMode(1024, 650), "Game");
Screen2::getInstance().Initiate("MainMenu");
Screen::getInstance().Initialize();

while (Gamewindow.isOpen()){




    sf::Event event;
    while(Gamewindow.pollEvent(event)) {

        if(event.type == sf::Event::Closed){
           Gamewindow.close();
        }
    }


    Gamewindow.clear(sf::Color::Blue);
    Screen::getInstance().updateScreen();
    Screen::getInstance().Draw();
    Gamewindow.display();

}

return 0;

}


Screen.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SCREEN_H
#define SCREEN_H
#include "Screen2.h"

class Screen: public Screen2
{
    public:
        Screen2 *CurrentScreen;
        static Screen &getInstance();
        void Initialize();
        void loadContent();
        void unloadContent();
        void updateScreen();
        void Draw();
};

#endif // SCREEN_H


Screen.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <SFML/Graphics.hpp>
#include "Screen.h"
#include "Screen2.h"
#include "Splashscreen.h"
#include "MainMenu.h"
#include <iostream>
#include <string>



Screen &Screen::getInstance(){
static Screen instance;
return instance;
}

void Screen::Initialize()
{
 if(setScreen == "MainMenu"){
    MainMenu menu;

    Screen2 *CurrentScreen = &menu;
}

}
void Screen::loadContent()
{
    CurrentScreen->loadContent();

}
void Screen::unloadContent()
{
    CurrentScreen->unloadContent();

}
void Screen::updateScreen()
{
    CurrentScreen->updateScreen();

}
void Screen::Draw()
{
    CurrentScreen->Draw();
}


Screen2.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef SCREEN2_H
#define SCREEN2_H
#include <string>


class Screen2
{
    public:
        std::string setScreen;
        static Screen2 &getInstance();
        void Initiate(std::string ScreenOption);
        virtual void loadContent();
        virtual void unloadContent();
        virtual void updateScreen();
        virtual void Draw();
};

#endif // SCREEN2_H


Screen2.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Screen2.h"
#include "Screen.h"
#include "MainMenu.h"
#include "Splashscreen.h"
#include <string>


Screen2 &Screen2::getInstance(){
static Screen2 instance2;
return instance2;
}
void Screen2::Initiate(std::string ScreenOption)
{
setScreen = ScreenOption;

}
void Screen2::loadContent(){}
void Screen2::unloadContent(){}
void Screen2::updateScreen(){}
void Screen2::Draw(){}


I know I didn't put the mainmenu class on here but I didn't think it was essential to figure out the problem seeing how there is no code yet just some functions.

If you see problems with my code please don't give me a hard time because I'm new to game programming and just trying to find ways that work and ways that don't.
Thank you for your time and feedback :).
Last edited on
In your "Screen.cpp", on line 21, you are shadowing the variable within the class by creating a new instance of the class. This means that the pointer that your other functions are accessing points to junk, causing your Segmentation Fault. Try changing it to this instead:
1
2
3
this->CurrentScreen = new MainMenu;
// You don't need the "this->", just showing it so you remember its not
// a variable created within the function, but within the class. 


You need to use "new" because otherwise "menu" has gone out of scope, and hence been cleaned out of memory, which will also give you a segfault.

Also, you need to a destructor and constructor to the class, so that memory is allocated and cleaned properly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Screen : public Screen2 {
    private:
        Screen2* CurrentScreen;
        //...

    public:
        // Constructor to initialize CurrentScreen to be nullptr
        Screen() : CurrentScreen(nullptr) {}

        // Destructor to clean the memory when Screen gets deleted
        ~Screen() { if (CurrentScreen) delete CurrentScreen; }

        //...
};
Last edited on
Thanks for your help! I would've never solved that myself. I will try not to make that mistake again in the future!
Topic archived. No new replies allowed.