diff --git a/src/dlangui/core/events.d b/src/dlangui/core/events.d index 3caa30de..5c3358ab 100644 --- a/src/dlangui/core/events.d +++ b/src/dlangui/core/events.d @@ -1649,6 +1649,25 @@ class RunnableEvent : CustomEvent { } } +/** +Queue destroy event. + +This event allows delayed widget destruction and is used internally by +$(LINK2 $(DDOX_ROOT_DIR)dlangui/platforms/common/platform/Window.queueWidgetDestroy.html, Window.queueWidgetDestroy()). +*/ +class QueueDestroyEvent : RunnableEvent { + private Widget _widgetToDestroy; + this (Widget widgetToDestroy) + { + _widgetToDestroy = widgetToDestroy; + super(1,null, delegate void () { + if (_widgetToDestroy.parent) + _widgetToDestroy.parent.removeChild(_widgetToDestroy); + destroy(_widgetToDestroy); + }); + } +} + interface CustomEventTarget { /// post event to handle in UI thread (this method can be used from background thread) void postEvent(CustomEvent event); diff --git a/src/dlangui/platforms/common/platform.d b/src/dlangui/platforms/common/platform.d index 63629585..399505c3 100644 --- a/src/dlangui/platforms/common/platform.d +++ b/src/dlangui/platforms/common/platform.d @@ -519,7 +519,24 @@ class Window : CustomEventTarget { destroy(_timerQueue); _eventList = null; } - + + /** + Allows queue destroy of widget. + + Sometimes when you have very complicated UI with dynamic create/destroy lists of widgets calling simple destroy() + on widget makes segmentation fault. + + Usually because you destroy widget that on some stage call another that tries to destroy widget that calls it. + When the control flow returns widget not exist and you have seg. fault. + + This function use internally $(LINK2 $(DDOX_ROOT_DIR)dlangui/core/events/QueueDestroyEvent.html, QueueDestroyEvent). + */ + void queueWidgetDestroy(Widget widgetToDestroy) + { + QueueDestroyEvent ev = new QueueDestroyEvent(widgetToDestroy); + postEvent(ev); + } + private void animate(Widget root, long interval) { if (root is null) return;