Skip to content

Commit

Permalink
Implemented delayed destruction of Dialog in MenuManager to avoid "de…
Browse files Browse the repository at this point in the history
…lete this" type problems
  • Loading branch information
Grumbel committed Aug 26, 2014
1 parent 575ee78 commit 9f72741
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/gui/menu_manager.cpp
Expand Up @@ -136,6 +136,8 @@ class MenuTransition

MenuManager::MenuManager() :
m_dialog(),
m_has_next_dialog(false),
m_next_dialog(),
m_menu_stack(),
m_transition(new MenuTransition)
{
Expand Down Expand Up @@ -190,6 +192,12 @@ MenuManager::event(const SDL_Event& ev)
void
MenuManager::draw(DrawingContext& context)
{
if (m_has_next_dialog)
{
m_dialog = std::move(m_next_dialog);
m_has_next_dialog = false;
}

if (m_transition->is_active())
{
m_transition->update();
Expand Down Expand Up @@ -222,7 +230,10 @@ MenuManager::draw(DrawingContext& context)
void
MenuManager::set_dialog(std::unique_ptr<Dialog> dialog)
{
m_dialog = std::move(dialog);
// delay reseting m_dialog to a later point, as otherwise the Dialog
// can't unset itself without ending up with "delete this" problems
m_next_dialog = std::move(dialog);
m_has_next_dialog = true;
}

void
Expand Down
5 changes: 4 additions & 1 deletion src/gui/menu_manager.hpp
Expand Up @@ -17,9 +17,9 @@
#ifndef HEADER_SUPERTUX_GUI_MENU_MANAGER_HPP
#define HEADER_SUPERTUX_GUI_MENU_MANAGER_HPP

#include <vector>
#include <list>
#include <memory>
#include <vector>

#include "SDL.h"

Expand All @@ -37,6 +37,9 @@ class MenuManager

private:
std::unique_ptr<Dialog> m_dialog;
bool m_has_next_dialog;
std::unique_ptr<Dialog> m_next_dialog;

std::vector<std::unique_ptr<Menu> > m_menu_stack;
std::unique_ptr<MenuTransition> m_transition;

Expand Down

0 comments on commit 9f72741

Please sign in to comment.