demo memory leak fix

This commit is contained in:
h4570
2022-07-31 11:52:49 +02:00
parent 347fdb240b
commit 834d57ea32
10 changed files with 242 additions and 62 deletions
+44 -8
View File
@@ -19,21 +19,57 @@ namespace Demo {
template <typename StateTypeT>
class StateManager {
public:
StateManager(const StateTypeT& initialState) {
StateManager(const StateTypeT& t_initialState,
const StateTypeT& t_exitState) {
stateInitialized = false;
currentState = initialState;
currentState = t_initialState;
exitState = t_exitState;
}
~StateManager() {
for (auto& state : states) {
delete state;
}
}
~StateManager() { freeAll(); }
const StateTypeT& getCurrentState() const { return currentState; }
const StateTypeT& getExitState() const { return exitState; }
bool finished() const { return currentState == exitState; }
std::vector<State<StateTypeT>*>* getAll() { return &states; }
State<StateTypeT>* get(const StateTypeT& stateType) {
for (auto& state : states) {
if (state->getState() == stateType) {
return state;
}
}
return nullptr;
}
void add(State<StateTypeT>* state) { states.push_back(state); }
/** remove (without free) */
void remove(const State<StateTypeT>* state) {
states.erase(std::remove(states.begin(), states.end(), state),
states.end());
}
/** free and remove */
void free(const State<StateTypeT>* state) {
auto* found = get(state);
if (!found) return;
remove(found);
delete found;
}
/** free and remove all */
void freeAll() {
for (auto& state : states) {
delete state;
}
states.clear();
}
void update() {
for (auto& state : states) {
if (state->getState() == currentState) {
@@ -57,7 +93,7 @@ class StateManager {
private:
bool stateInitialized;
std::vector<State<StateTypeT>*> states;
StateTypeT currentState;
StateTypeT currentState, exitState;
};
} // namespace Demo