I'm trying to pass an sf::Texture& to a class constructor. I have no compiler warnings, but the program crashes immediately with Segmentation fault: 11.
Class::Class(sf::Texture& pTexture) {
sprite.setTexture(pTexture);
}
class = make_unique<Class>(texture);
I am able to pass other data types.
Edit: Here is a summarized version of the code that I'm using.
//Declaration
class Banner {
public:
Banner();
Banner(sf::Texture& pTexture);
Banner(const Banner&);
~Banner();
private:
sf::Sprite bannerSprite;
};
//Definition
Banner::Banner(sf::Texture& pTexture)
{
bannerSprite.setTexture(pTexture);
}
//Declaration in class MainMenu
std::unique_ptr<Banner> _titleBanner;
//Calling the constructor
MainMenu::MainMenu ( Resources& pResources) : _resources(pResources) {
_titleBanner = std::make_unique<Banner>(_resources.texture("ButtonBG"));
}
CodePudding user response:
Had you tried run program in release mode, SFML usualy crash in debug mode.
CodePudding user response:
The issue was trying to use the _resources.texture(...)
before it had been loaded. Therefore, when MainMenu
tried to pass _resources.texture(...)
to bannerSprite
, the texture wasn't there, hence the SegFault.
The classes Resources
and MainMenu
are both initialized in the same constructor. However, I was calling _resources.load()
after initialization. I moved Resources::load()
into the Resources
constructor and it works!