Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Segmentation fault trying to return sf::String from class  (Read 1845 times)

0 Members and 1 Guest are viewing this topic.

aquaglow

  • Newbie
  • *
  • Posts: 15
    • View Profile
Segmentation fault trying to return sf::String from class
« on: July 08, 2011, 09:15:52 pm »
I've been trying to find a solution for a couple of hours, but having no luck :?

After advice earlier, I rewrote one of my simple struct's as a proper class, but now get a segmentation fault when trying to return a sf::String it stores.

My class:

Code: [Select]
namespace sw {

    class Hitpoints {
    private:
        sf::String string;
        int framesLeft;
        int fadeRate;
        float shrinkRate;
        bool isExpired;
    public:
        Hitpoints(sf::Unicode::Text, sf::Font, int, sf::Color, int, int, int, int, float);
        void update();
        sf::String getString();
        bool hasExpired();
    };

    Hitpoints::Hitpoints(sf::Unicode::Text text, sf::Font font, int fontSize, sf::Color color, int centreX, int centreY, int framesL, int fadeR, float shrinkR) {
        this->string = sf::String(text, font, fontSize);
        this->string.SetColor(color);
        sf::FloatRect rect(this->string.GetRect());
        this->string.SetPosition(centreX - (rect.GetWidth() / 2), centreY - (rect.GetHeight() / 2));
        this->framesLeft = framesL;
        this->fadeRate = fadeR;
        this->shrinkRate = shrinkR;
        isExpired = false;
    }

    void Hitpoints::update() {
        sf::Color color = this->string.GetColor();
        this->string.SetColor(sf::Color(color.r, color.g, color.b, color.a - this->fadeRate));

        this->string.SetSize(this->string.GetSize() - shrinkRate);

        this->framesLeft = this->framesLeft - 1;
        if (this->framesLeft == 0) {
            this->isExpired = true;
        }
    }

    sf::String Hitpoints::getString() {
        return this->string;
    }

    bool Hitpoints::hasExpired() {
        return this->isExpired;
    }
}


In my main function I first add to the list:

Code: [Select]
sw::Hitpoints hitpoint("+10", scoreFont, 18, sf::Color(255, 234, 0), bulletVec2.x, bulletVec2.y, 60, 4, 0.2);
hitpoints.push_back(hitpoint);


And then later I try to display it:

Code: [Select]
std::list<sw::Hitpoints>::iterator hps = hitpoints.begin();
while (hps != hitpoints.end()) {
    App.Draw(hps->getString());
    hps++;
}


I've run a backtrace in gdb, and get the segmentation fault on the line App.Draw(hps->getString());

Code: [Select]
(gdb) bt
#0  0x0013a377 in sf::Font::GetCharacterSize() const ()
   from /usr/lib/libsfml-graphics.so.1.6
#1  0x00139edb in sf::Drawable::Draw(sf::RenderTarget&) const ()
   from /usr/lib/libsfml-graphics.so.1.6
#2  0x00146730 in sf::RenderTarget::Draw(sf::Drawable const&) ()
   from /usr/lib/libsfml-graphics.so.1.6
#3  0x0804e2b3 in main () at spacewar.cpp:466


I've tried returning a reference to the sf::String object (perhaps wrongly) instead, but the same thing happens.