将自定义可绘制对象存储在数组中导致绘制时出现分段错误

Storing Custom Drawables in Array Causes Segmentation Error When Drawing Them

我有一个名为 levelButtonsstd::vector<Button>,它像这样存储 Button

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);
    }
}

然后我以后像这样画它们:

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

其中 winRenderWindow*。 class 按钮如下所示:

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);
}

它抛出一个

Segmentation Fault: 11;

报告如下所示:

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

为什么会抛出分段错误?我很确定我的迭代没问题,而且我的 Button class 单独工作 - 我创建了一个主文件来测试它并且它工作......

我修好了。我只是简单地添加了这段代码来加载精灵:

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

并使用了它。感谢您的帮助。