Skip to content

Commit

Permalink
Fixed segfault on closing.
Browse files Browse the repository at this point in the history
  • Loading branch information
babyccino committed Jan 9, 2018
1 parent 9150550 commit f53f592
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions include/radix/input/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Channel: public ChannelBase<T>, ChannelListener {
void init(const int &id, EventDispatcher &event, const std::vector<Bind> &binds);
void reInit(EventDispatcher &event);

void close();

virtual void channelChanged(const int &id) override;

private:
Expand All @@ -48,6 +50,8 @@ class SubChannel: public ChannelBase<T> {
void init(const int &id, EventDispatcher &event, const Bind &bind);
void reInit(EventDispatcher &event);

void close();

private:
void addObservers(EventDispatcher &event);

Expand Down
2 changes: 2 additions & 0 deletions include/radix/input/InputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class InputManager : public ChannelListener {
void init();
void reInit();

void close();

virtual void channelChanged(const int &id) override;
Vector2f getPlayerMovementVector() const;

Expand Down
7 changes: 4 additions & 3 deletions source/BaseGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ void BaseGame::removeHook() { }
void BaseGame::customTriggerHook() { }

void BaseGame::cleanUp() {
removeHook();
setWorld({});
window.close();
removeHook();
inputManager.close();
setWorld({});
window.close();
}

void BaseGame::render() {
Expand Down
14 changes: 14 additions & 0 deletions source/input/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ void Channel<T>::reInit(EventDispatcher &event) {
}
}

template<class T>
void Channel<T>::close() {
for (SubChannel<T> &subChannel : subChannels) {
subChannel.close();
}
}

template<class T>
void Channel<T>::channelChanged(const int &id) {
this->Channel<T>::set(this->subChannels.at(id).SubChannel<T>::get());
Expand Down Expand Up @@ -79,6 +86,13 @@ void SubChannel<T>::reInit(EventDispatcher &event) {
this->addObservers(event);
}

template<class T>
void SubChannel<T>::close() {
for (EventDispatcher::CallbackHolder& callback : callbacks) {
callback.removeThis();
}
}

template<class T>
void SubChannel<T>::addObservers(EventDispatcher &event) {
switch (bind.inputType) {
Expand Down
10 changes: 10 additions & 0 deletions source/input/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ void InputManager::reInit() {
}
}

void InputManager::close() {
for (auto& el : analogueChannels) {
el.second.close();
}

for (auto& el : digitalChannels) {
el.second.close();
}
}

void InputManager::channelChanged(const int &id) {
entities::Player& player = game.getWorld()->getPlayer();

Expand Down

0 comments on commit f53f592

Please sign in to comment.