Small mistake. Didn't maintain the texture pointer in the sprite so it could go out of scope and crash with use after free.

This commit is contained in:
Zed A. Shaw 2026-03-11 07:39:50 -04:00
parent 516f20124f
commit e88179b788
3 changed files with 8 additions and 3 deletions

View file

@ -30,6 +30,7 @@ namespace guecs {
bool stretch = true; bool stretch = true;
bool is_icon = false; bool is_icon = false;
std::shared_ptr<sf::Sprite> sprite = nullptr; std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr;
void init(lel::Cell &cell); void init(lel::Cell &cell);
void update(const string& new_name); void update(const string& new_name);

View file

@ -10,7 +10,9 @@ project('lel-guecs', 'cpp',
]) ])
# use this for common options only for our executables # use this for common options only for our executables
cpp_args=[] cpp_args=[
'-Wno-deprecated-declarations'
]
link_args=[] link_args=[]
# these are passed as override_defaults # these are passed as override_defaults
exe_defaults = [ 'warning_level=2' ] exe_defaults = [ 'warning_level=2' ]

View file

@ -59,7 +59,8 @@ namespace guecs {
if(new_name != name) { if(new_name != name) {
name = new_name; name = new_name;
auto sprite_texture = BACKEND->get_sprite(name); auto sprite_texture = BACKEND->get_sprite(name);
sprite->setTexture(*sprite_texture.texture); texture = sprite_texture.texture;
sprite->setTexture(*texture);
sprite->setTextureRect({{0,0},sprite_texture.frame_size}); sprite->setTextureRect({{0,0},sprite_texture.frame_size});
} }
} }
@ -71,7 +72,8 @@ namespace guecs {
auto bounds = sprite_texture.frame_size; auto bounds = sprite_texture.frame_size;
sf::IntRect rect{{0,0}, bounds}; sf::IntRect rect{{0,0}, bounds};
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect); texture = sprite_texture.texture;
sprite = make_shared<sf::Sprite>(*texture, rect);
if(stretch) { if(stretch) {
sprite->setScale({ sprite->setScale({