Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dangling child widgets from Screen's focus/drag #389

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nanogui/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class NANOGUI_EXPORT Screen : public Widget {

/* Internal helper functions */
void updateFocus(Widget *widget);
void disposeWindow(Window *window);
void disposeWidget(Widget *widget);
void centerWindow(Window *window);
void moveWindowToFront(Window *window);
void drawWidgets();
Expand Down
2 changes: 1 addition & 1 deletion python/py_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,7 @@ static const char *__doc_nanogui_Screen_charCallbackEvent = R"doc()doc";

static const char *__doc_nanogui_Screen_cursorPosCallbackEvent = R"doc()doc";

static const char *__doc_nanogui_Screen_disposeWindow = R"doc()doc";
static const char *__doc_nanogui_Screen_disposeWidget = R"doc(Remove Screen's references to a widget and its children)doc";
Copy link
Collaborator

@svenevs svenevs Apr 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please un-do changes to this file. If we want it documented, it must be done in include/nanogui/screen.h and then run make mkdoc (that will regenerate all python docs). Personally I think it's OK to leave it undocumented for now at least ;)

So to clarify: all you should need to do is run make mkdoc or ninja mkdoc, it's a special target added at the bottom of CMakeLists.txt. If you need help with that let me know :)


static const char *__doc_nanogui_Screen_drawAll = R"doc(Draw the Screen contents)doc";

Expand Down
10 changes: 6 additions & 4 deletions src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,14 @@ void Screen::updateFocus(Widget *widget) {
moveWindowToFront((Window *) window);
}

void Screen::disposeWindow(Window *window) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), window) != mFocusPath.end())
void Screen::disposeWidget(Widget *widget) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), widget) != mFocusPath.end())
mFocusPath.clear();
if (mDragWidget == window)

if (mDragWidget == widget) {
mDragWidget = nullptr;
removeChild(window);
mDragActive = false;
}
}

void Screen::centerWindow(Window *window) {
Expand Down
5 changes: 5 additions & 0 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Widget::Widget(Widget *parent)
}

Widget::~Widget() {
try {
screen()->disposeWidget(this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing this kind of exception stuff in the destructor may not be a good decision. I think there are two good options:

  1. Make screen() able to return nullptr rather than throwing an std::runtime_error. That's kind of a significant change though.

  2. Less pervasive,just do it manually -- the Widget destructor is a special case, exceptions + destructors is dangerous territory. I think we could just do this:

    Widget::~Widget() {
        /* Similar to Widget::screen(), but does not throw:
         * 1. Destruction of orphan widgets should not throw.
         * 2. Special case: every Screen is a Widget.
         */
        Screen *screen = dynamic_cast<Screen *>(this);
        if (screen == nullptr) { // only dispose widgets, screen cannot dispose itself
            Widget *widget = this;
            while (true) {
                if (!widget) break;// 1: orphan widgets (no parent screen)
                screen = dynamic_cast<Screen *>(widget);
                if (screen) break;
                widget = widget->parent();
            }
            if (screen)
                screen->disposeWidget(this);
        }
    
        for (auto child : mChildren) {
            if (child)
                child->decRef();
        }
    }

Basically, in the destructor we need to be extra careful. "Orphan" widgets (no screen in parent chain) are not exactly supported, but not explicitly forbidden. No orphan ever gets drawn until it has a screen in the parent chain, so all drawing code assumes that a screen() can be found. We just want to make sure that we don't crash on what you mentioned (when it's a Screen being destructed), but also in the rare (but possibly abusively used) orphan widget case.

}
catch (const std::runtime_error&) {}

for (auto child : mChildren) {
if (child)
child->decRef();
Expand Down
2 changes: 1 addition & 1 deletion src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void Window::dispose() {
Widget *widget = this;
while (widget->parent())
widget = widget->parent();
((Screen *) widget)->disposeWindow(this);
((Screen *) widget)->removeChild(this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work. Run Example 1 and use the Info / Warn / Ask dialog. That's the "real" test case for Window::dispose (gets called by the message diaolgs in src/messagedialog.cpp).

}

void Window::center() {
Expand Down