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

sf::Drawable
in a
std::vector
.
The problem is, the
sf::Drawable
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
std::vector<std::vector<sf::Drawable*>>
, 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[coat].push_back(sprite);

else
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
{
public:
Page();

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

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

protected:
std::vector<std::vector<sf::Drawable*>> m_sprites;
private:
};

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

LoadSprites();

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

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

window.clear(sf::Color::White);

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++) {
window.draw((*(pageSprites->at(i)[j])));
}
}

window.display();
}
}

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
{
public:
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;

protected:

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 (
std::unique_ptr<sf::Drawable>
) but I don't know how to use them, because I can't use
std::make_unique<sf::Drawable>
because of the abstraction of
sf::Drawable
).

Answer Source

With

{
    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