Search code examples
c++segmentation-faultdrawablesfml

Storing Custom Drawables in Array Causes Segmentation Error When Drawing Them


I have a std::vector<Button> called levelButtonswhich stores Buttons like this:

for (int i = 1; i <= 8; i++) {
    for (int j = 0; j < 4; j++) {
        Button level(50, 50, std::bind(&Game::continueGame, this, (j * 8) + i), " " + std::to_string((j * 8) + i), &levelTexture);
        level.setPosition(50 + (i * 100), 200 + (j * 100));
        levelButtons.push_back(level);
    }
}

And then I draw them later like this:

for (int i=0; i<levelButtons.size(); i++) {
    win->draw(levelButtons[i]);
}

Where win is a RenderWindow*. The class Button looks like this:

Button::Button(float width, float height, std::function<void()> onclick, String face, Color fillColour) {
    arial.loadFromFile("/Users/mmysteriouss/Downloads/arial.ttf");
    pointerTexture.loadFromFile("/Users/mmysteriouss/Downloads/pointer.png");
    pointer.setTexture(pointerTexture);
    pointer.scale(0.04, 0.04);
    buttonface.setFont(arial);
    buttonface.setString(face);
    buttonface.setCharacterSize(30);
    buttonface.setFillColor(Color::Black);
    outline.setSize(Vector2f(width, height));
    outline.setOutlineColor(Color::Black);
    outline.setFillColor(fillColour);
    click = onclick;
}

Button::Button(float width, float height, std::function<void()> onclick, String face, Texture *fillTexture) {
    arial.loadFromFile("/Users/mmysteriouss/Downloads/arial.ttf");
    pointerTexture.loadFromFile("/Users/mmysteriouss/Downloads/pointer.png");
    pointer.setTexture(pointerTexture);
    pointer.scale(0.04, 0.04);
    buttonface.setFont(arial);
    buttonface.setString(face);
    buttonface.setCharacterSize(30);
    buttonface.setFillColor(Color::White);
    outline.setSize(Vector2f(width, height));
    outline.setOutlineColor(Color::Black);
    outline.setTexture(fillTexture);
    click = onclick;
}

void Button::handleClick(float x, float y) {
    if (outline.getGlobalBounds().contains(x, y)) {
        click();
    }
}

void Button::setPosition(float x, float y) {
    outline.setPosition(x, y);
    buttonface.setPosition(x, y);
}

void Button::handleMove(float x, float y) {
    hovering = outline.getGlobalBounds().contains(x, y);
    pointer.setPosition(x - 15, y - 5);
}

And it throws a

Segmentation Fault: 11;

The report looks like this:

thread 0 Crashed:: Dispatch queue: com.apple.main-thread

0 libsfml-graphics.2.4.dylib 0x0000000107e0c6f7 sf::Font::setCurrentSize(unsigned int) const + 23

1 libsfml-graphics.2.4.dylib 0x0000000107e0c914 sf::Font::getUnderlinePosition(unsigned int) const + 30

2 libsfml-graphics.2.4.dylib 0x0000000107e35c30 sf::Text::ensureGeometryUpdate() const + 202

3 libsfml-graphics.2.4.dylib 0x0000000107e363aa sf::Text::draw(sf::RenderTarget&, sf::RenderStates) const + 38

4 libsfml-graphics.2.4.dylib 0x0000000107e29dfd sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) + 41

5 light 0x0000000107dcd081 Button::draw(sf::RenderTarget&, sf::RenderStates) const + 81 (Button.h:43)

6 libsfml-graphics.2.4.dylib 0x0000000107e29dfd sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) + 41

7 light 0x0000000107dd43c2 Game::draw() + 210 (Game.cpp:26)

8 light 0x0000000107dd4bc5 AbsGame::render() + 69 (AbsGame.h:44)

9 light 0x0000000107dd4910 main + 496 (main.cpp:27)

10 libdyld.dylib 0x00007fff76f58015 start + 1

Why does it throw a Segmentation Fault? I'm pretty sure my iteration is fine, and, my Button class works seperately - I created a main file to test it with and it worked...


Solution

  • I fixed it. I just simply added this code to load a sprite:

    sf::Sprite Button::loadSprite(std::string source) {
        static sf::Texture t;
        t.loadFromFile(source);
        static sf::Sprite s;
        s.setTexture(t);
        return s;
    }
    

    and used it. Thanks for your help.