Skip to content

Commit b49e5cf

Browse files
committedOct 24, 2014
Remove m_ext_ptr in GUIFormSpecMenu, replaced by refcount mechanism
1 parent 73bf791 commit b49e5cf

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed
 

‎src/game.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -944,9 +944,16 @@ static inline void create_formspec_menu(GUIFormSpecMenu** cur_formspec,
944944

945945
if (*cur_formspec == 0) {
946946
*cur_formspec = new GUIFormSpecMenu(device, guiroot, -1, &g_menumgr,
947-
invmgr, gamedef, tsrc, fs_src, txt_dest, cur_formspec, client);
947+
invmgr, gamedef, tsrc, fs_src, txt_dest, client);
948948
(*cur_formspec)->doPause = false;
949-
(*cur_formspec)->drop();
949+
950+
/*
951+
Caution: do not call (*cur_formspec)->drop() here --
952+
the reference might outlive the menu, so we will
953+
periodically check if *cur_formspec is the only
954+
remaining reference (i.e. the menu was removed)
955+
and delete it in that case.
956+
*/
950957
}
951958
else {
952959
(*cur_formspec)->setFormSource(fs_src);
@@ -3417,10 +3424,16 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
34173424
}
34183425

34193426
/*
3420-
make sure menu is on top
3427+
1. Delete formspec menu reference if menu was removed
3428+
2. Else, make sure formspec menu is on top
34213429
*/
3422-
if ((!noMenuActive()) && (current_formspec)) {
3430+
if (current_formspec) {
3431+
if (current_formspec->getReferenceCount() == 1) {
3432+
current_formspec->drop();
3433+
current_formspec = NULL;
3434+
} else if (!noMenuActive()) {
34233435
guiroot->bringToFront(current_formspec);
3436+
}
34243437
}
34253438

34263439
/*
@@ -3509,6 +3522,11 @@ void the_game(bool &kill, bool random_input, InputHandler *input,
35093522
g_menumgr.m_stack.front()->setVisible(false);
35103523
g_menumgr.deletingMenu(g_menumgr.m_stack.front());
35113524
}
3525+
if (current_formspec) {
3526+
current_formspec->drop();
3527+
current_formspec = NULL;
3528+
}
3529+
35123530
/*
35133531
Draw a "shutting down" screen, which will be shown while the map
35143532
generator and other stuff quits

‎src/guiEngine.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
190190
m_texture_source,
191191
m_formspecgui,
192192
m_buttonhandler,
193-
NULL, NULL);
193+
NULL);
194194

195195
m_menu->allowClose(false);
196196
m_menu->lockSize(true,v2u32(800,600));
@@ -220,8 +220,7 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
220220
}
221221

222222
m_menu->quitMenu();
223-
m_menu->remove();
224-
delete m_menu;
223+
m_menu->drop();
225224
m_menu = NULL;
226225
}
227226

‎src/guiFormSpecMenu.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
6868
/*
6969
GUIFormSpecMenu
7070
*/
71-
7271
GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
7372
gui::IGUIElement* parent, s32 id, IMenuManager *menumgr,
7473
InventoryManager *invmgr, IGameDef *gamedef,
7574
ISimpleTextureSource *tsrc, IFormSource* fsrc, TextDest* tdst,
76-
GUIFormSpecMenu** ext_ptr, Client* client) :
75+
Client* client) :
7776
GUIModalMenu(dev->getGUIEnvironment(), parent, id, menumgr),
7877
m_device(dev),
7978
m_invmgr(invmgr),
@@ -89,7 +88,6 @@ GUIFormSpecMenu::GUIFormSpecMenu(irr::IrrlichtDevice* dev,
8988
m_lock(false),
9089
m_form_src(fsrc),
9190
m_text_dst(tdst),
92-
m_ext_ptr(ext_ptr),
9391
m_font(dev->getGUIEnvironment()->getSkin()->getFont()),
9492
m_formspec_version(0)
9593
#ifdef __ANDROID__
@@ -130,11 +128,6 @@ GUIFormSpecMenu::~GUIFormSpecMenu()
130128
if (m_text_dst != NULL) {
131129
delete m_text_dst;
132130
}
133-
134-
if (m_ext_ptr != NULL) {
135-
assert(*m_ext_ptr == this);
136-
*m_ext_ptr = NULL;
137-
}
138131
}
139132

140133
void GUIFormSpecMenu::removeChildren()

‎src/guiFormSpecMenu.h

-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ class GUIFormSpecMenu : public GUIModalMenu
210210
ISimpleTextureSource *tsrc,
211211
IFormSource* fs_src,
212212
TextDest* txt_dst,
213-
GUIFormSpecMenu** ext_ptr,
214213
Client* client
215214
);
216215

@@ -346,7 +345,6 @@ class GUIFormSpecMenu : public GUIModalMenu
346345
private:
347346
IFormSource *m_form_src;
348347
TextDest *m_text_dst;
349-
GUIFormSpecMenu **m_ext_ptr;
350348
gui::IGUIFont *m_font;
351349
unsigned int m_formspec_version;
352350

‎src/modalMenu.h

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class GUIModalMenu : public gui::IGUIElement
9696
9797
WARNING: THIS DEALLOCATES THE MENU FROM MEMORY. Return
9898
immediately if you call this from the menu itself.
99+
100+
(More precisely, this decrements the reference count.)
99101
*/
100102
void quitMenu()
101103
{

0 commit comments

Comments
 (0)
Please sign in to comment.