FeelZoR FeelZoR - 4 years ago 87
C++ Question

Create a class which contains the items to draw (seg fault)

I'm struggling with a code that I think... should be very easy. But I don't know why, I don't understand how to make it correctly.

I'm trying to store all instances of

in a
The problem is, the
class is abstract, so I have to use pointers, I know it, it's correct.

I'm trying to render the items part by part, so I'm using
, and if you have any idea on how to make something better than it, I'll gladly accept it, because I hate vectors of vectors, it looks dangerously big, and I'm sure it uses a lot of memory (I try to keep it to 3 parts max).

The problem is, when I add a pointer in the Page class (where I store all the pointers to the drawable things), it causes a segfault if the sprite goes out of scope, which is normal. The thing is, I don't know how to prevent the program from deleting the sprite, because I don't want my main class (named Game) to keep the sprites, texts, ...

Here is the code, it should be clearer :

Page.cpp :

#include "Page.h"

void Page::AddSprite(int coat, sf::Drawable* sprite) {

if (coat < m_sprites.size())

m_sprites.push_back(std::vector<sf::Drawable*>(1, sprite));

std::vector<std::vector<sf::Drawable*>>* Page::GetSprites() {
return &m_sprites;

Page.h :

#ifndef PAGE_H
#define PAGE_H

#include <SFML/Graphics.hpp>
#include <vector>
#include <memory>

class Page

void AddSprite(int coat, sf::Drawable* sprite);

std::vector<std::vector<sf::Drawable*>>* GetSprites();

std::vector<std::vector<sf::Drawable*>> m_sprites;

#endif // PAGE_H

Game.cpp :

Game::Game() : m_numberItems(20) {}

void Game::Run() {
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Lost Items | Items remaining : " + std::to_string(m_numberItems));


while (window.isOpen()) {
sf::Event event;

while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)


std::vector<std::vector<sf::Drawable*>>* pageSprites = m_page.GetSprites();
for (int i = 0; i < pageSprites->size(); i++) {
for (int j = 0; j < pageSprites->at(i).size(); j++) {


void Game::LoadSprites() {
int CASE_LEN = CASE_LENGTH; // It was after a copy paste from another class, and I didn't want to change all the CASE_LEN to CASE_LENGTH
for (short i = 0; i < m_numberItems; i++) {
int x = rand() % ((SCREEN_WIDTH - CASE_LEN) / CASE_LEN);
int y = rand() % ((SCREEN_HEIGHT - CASE_LEN) / CASE_LEN);
sf::Sprite sprite(m_graphics.GetTilesTexture());
sprite.setTextureRect(sf::IntRect(ITEM_X, ITEM_Y, CASE_LEN, CASE_LEN));
sprite.setPosition(x * CASE_LEN, y * CASE_LEN);
m_page.AddSprite(0, &sprite);

Game.h :

#ifndef GAME_H
#define GAME_H

#include <SFML/Graphics.hpp>
#include "Page.h"

class Game
void Run();
void LoadSprites();

/** @brief The window width.*/
static const unsigned short SCREEN_WIDTH = 1280;

/** @brief The window height */
static const unsigned short SCREEN_HEIGHT = 736;

/** @brief The case length. */
static const unsigned short CASE_LENGTH = 32;


unsigned char m_numberItems;
Page m_page;

#endif // GAME_H

I hope I've been clear enough, and thank you for your answers !

P.S. : I thought I could use unique pointers (
) but I don't know how to use them, because I can't use
because of the abstraction of

Answer Source


    sf::Sprite sprite(m_graphics.GetTilesTexture());

    // ...
    m_page.AddSprite(0, &sprite);

You store local variable and so have dangling reference at end of the scope.

You may do something like (using smart pointer)

    auto sprite = std::make_unique<sf::Sprite>(m_graphics.GetTilesTexture());

    // ...
    m_page.AddSprite(0, std::move(sprite));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download