Skip to content

Commit

Permalink
Remove m_ext_ptr in GUIFormSpecMenu, replaced by refcount mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
kahrl committed Oct 24, 2014
1 parent 73bf791 commit b49e5cf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
26 changes: 22 additions & 4 deletions src/game.cpp
Expand Up @@ -944,9 +944,16 @@ static inline void create_formspec_menu(GUIFormSpecMenu** cur_formspec,

if (*cur_formspec == 0) {
*cur_formspec = new GUIFormSpecMenu(device, guiroot, -1, &g_menumgr,
invmgr, gamedef, tsrc, fs_src, txt_dest, cur_formspec, client);
invmgr, gamedef, tsrc, fs_src, txt_dest, client);
(*cur_formspec)->doPause = false;
(*cur_formspec)->drop();

/*
Caution: do not call (*cur_formspec)->drop() here --
the reference might outlive the menu, so we will
periodically check if *cur_formspec is the only
remaining reference (i.e. the menu was removed)
and delete it in that case.
*/
}
else {
(*cur_formspec)->setFormSource(fs_src);
Expand Down Expand Up @@ -3417,10 +3424,16 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
}

/*
make sure menu is on top
1. Delete formspec menu reference if menu was removed
2. Else, make sure formspec menu is on top
*/
if ((!noMenuActive()) && (current_formspec)) {
if (current_formspec) {
if (current_formspec->getReferenceCount() == 1) {
current_formspec->drop();
current_formspec = NULL;
} else if (!noMenuActive()) {
guiroot->bringToFront(current_formspec);
}
}

/*
Expand Down Expand Up @@ -3509,6 +3522,11 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
g_menumgr.m_stack.front()->setVisible(false);
g_menumgr.deletingMenu(g_menumgr.m_stack.front());
}
if (current_formspec) {
current_formspec->drop();
current_formspec = NULL;
}

/*
Draw a "shutting down" screen, which will be shown while the map
generator and other stuff quits
Expand Down
5 changes: 2 additions & 3 deletions src/guiEngine.cpp
Expand Up @@ -190,7 +190,7 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
m_texture_source,
m_formspecgui,
m_buttonhandler,
NULL, NULL);
NULL);

m_menu->allowClose(false);
m_menu->lockSize(true,v2u32(800,600));
Expand Down Expand Up @@ -220,8 +220,7 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
}

m_menu->quitMenu();
m_menu->remove();
delete m_menu;
m_menu->drop();
m_menu = NULL;
}

Expand Down
9 changes: 1 addition & 8 deletions src/guiFormSpecMenu.cpp
Expand Up @@ -68,12 +68,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
/*
GUIFormSpecMenu
*/

GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
InventoryManager *invmgr, IGameDef *gamedef,
ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
GUIFormSpecMenu** ext_ptr, Client* client) :
Client* client) :
GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
m_device(dev),
m_invmgr(invmgr),
Expand All @@ -89,7 +88,6 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
m_lock(false),
m_form_src(fsrc),
m_text_dst(tdst),
m_ext_ptr(ext_ptr),
m_font(dev->getGUIEnvironment()->getSkin()->getFont()),
m_formspec_version(0)
#ifdef __ANDROID__
Expand Down Expand Up @@ -130,11 +128,6 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
if (m_text_dst != NULL) {
delete m_text_dst;
}

if (m_ext_ptr != NULL) {
assert(*m_ext_ptr == this);
*m_ext_ptr = NULL;
}
}

void GUIFormSpecMenu::removeChildren()
Expand Down
2 changes: 0 additions & 2 deletions src/guiFormSpecMenu.h
Expand Up @@ -210,7 +210,6 @@ class GUIFormSpecMenu : public GUIModalMenu
ISimpleTextureSource *tsrc,
IFormSource* fs_src,
TextDest* txt_dst,
GUIFormSpecMenu** ext_ptr,
Client* client
);

Expand Down Expand Up @@ -346,7 +345,6 @@ class GUIFormSpecMenu : public GUIModalMenu
private:
IFormSource *m_form_src;
TextDest *m_text_dst;
GUIFormSpecMenu **m_ext_ptr;
gui::IGUIFont *m_font;
unsigned int m_formspec_version;

Expand Down
2 changes: 2 additions & 0 deletions src/modalMenu.h
Expand Up @@ -96,6 +96,8 @@ class GUIModalMenu : public gui::IGUIElement
WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
immediately if you call this from the menu itself.
(More precisely, this decrements the reference count.)
*/
void quitMenu()
{
Expand Down

0 comments on commit b49e5cf

Please sign in to comment.