My class load_game_state
(definition) has a member variable _saveGameItems
of type vector<shared_ptr<SaveGameState>>
and is never initialised (which it shouldn't need to).
Upon trying to use push_back()
or emplace_back()
on this vector I get a EXC_BAD_ACCESS
error. I do not understand why I can not add objects to this vector.
The SaveGameData
objects that populate the vector saves
. Get constructed correctly, each object contains three strings (namely a path, a description an a timestamp (which is actually just a string)).
My code:
load_game_state.cpp
vector<std::string> LoadGameState::getFilePathsOfSaveGames() {
JSONParser jp = JSONParser {};
vector<string> paths = get_all("assets/saves/", ".json");
vector<shared_ptr<SaveGameItem>> saves;
// Extract data
for(auto path : paths) {
json tmpData = jp.getSaveGame(path);
saves.emplace_back(make_shared<SaveGameItem>(SaveGameItem(path, tmpData.at("saveTime"), tmpData.at("name"))));
}
_saveGameItems = saves;
}
load_game_state.h
#ifndef RISKY_LOAD_GAME_STATE_H
#define RISKY_LOAD_GAME_STATE_H
#include "state.h"
#include "../risky.h"
#include "../scenes/load_game_scene.h"
#include "load_game_controller.h"
#include "../models/SaveGameItem.h"
#include <boost/filesystem.hpp>
namespace fs = ::boost::filesystem;
class LoadGameState : public State, public LoadGameController {
public:
LoadGameState(Risky& context, GameEngine& engine, StateManager* stateManager);
BaseScene& getScene() override;
void doQuit() override;
void handleInput() override;
void update() override;
~LoadGameState();
vector<string> get_all(const boost::filesystem::path& root, const string& ext);
const vector<shared_ptr<SaveGameItem>> &get_saveGameItems() const;
void set_saveGameItems(const vector<shared_ptr<SaveGameItem>> &_saveGameItems);
// TODO get file paths of all JSON files
vector<std::string> getFilePathsOfSaveGames() override;
// TODO parse into SaveGameItem objects
// TODO display these objects
private:
LoadGameScene _scene;
Risky& _riskyRef;
GameEngine& _engineRef;
// SaveGameItems
vector<shared_ptr<SaveGameItem>> _saveGameItems;
};
#endif //RISKY_LOAD_GAME_STATE_H
SaveGameItem looks as follows:
class SaveGameItem {
public:
SaveGameItem();
SaveGameItem(std::string filePath, std::string timestamp, std::string name);
const std::string &get_filePath() const;
void set_filePath(const std::string &_filePath);
const std::string &get_timestamp() const;
void set_timestamp(const std::string &_timestamp);
const std::string &get_name() const;
void set_name(const std::string &_name);
~SaveGameItem();
private:
std::string _filePath;
std::string _timestamp;
std::string _name;
};
Found the issue. _saveGameItems
hadn't been initialised yet as the constructor of the class that called getFilePathsOfSaveGames()
was called in the constructor of another class preventing _saveGameItems
from being initialised.