Repository: slint-ui/slint (language: rust)

Issue:
crash on application exit with Qt
### Bug Description

Hello, quitting my application crashes.
Does not happen with winit.

Here's the backtrace
```
(lldb) bt
* thread #1, name = 'chess_studio', stop reason = signal SIGSEGV: address not mapped to object (fault address: 0x0)
  * frame #0: 0x00007ffff76c2312 libQt6Core.so.6`QThreadStorageData::get() const + 66
    frame #1: 0x00007ffff6eed032 libQt6Gui.so.6`QOpenGLContext::currentContext() + 50
    frame #2: 0x00007ffff6b40ea1 libQt6Gui.so.6`QSurface::~QSurface() + 33
    frame #3: 0x00007ffff6b4ec72 libQt6Gui.so.6`QWindow::~QWindow() + 210
    frame #4: 0x00007ffff62b6c65 libQt6Widgets.so.6`QWidgetWindow::~QWidgetWindow() + 21
    frame #5: 0x00007ffff6282827 libQt6Widgets.so.6`QWidgetPrivate::deleteTLSysExtra() + 55
    frame #6: 0x00007ffff6296a98 libQt6Widgets.so.6`QWidget::destroy(bool, bool) + 312
    frame #7: 0x00007ffff629e9b0 libQt6Widgets.so.6`QWidget::~QWidget() + 928
    frame #8: 0x0000555556b11914 chess_studio`SlintWidget::~SlintWidget(this=0x00005555582df570) at qt_window.rs:87:12
    frame #9: 0x0000555556b11936 chess_studio`SlintWidget::~SlintWidget(this=0x00005555582df570) at qt_window.rs:87:12
    frame #10: 0x00007ffff754b8ae libQt6Core.so.6`QObject::event(QEvent*) + 78
    frame #11: 0x00007ffff623d9b8 libQt6Widgets.so.6`QApplicationPrivate::notify_helper(QObject*, QEvent*) + 136
    frame #12: 0x00007ffff74f3590 libQt6Core.so.6`QCoreApplication::notifyInternal2(QObject*, QEvent*) + 336
    frame #13: 0x00007ffff74f6f51 libQt6Core.so.6`QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) + 817
    frame #14: 0x00007ffff76b55c9 libQt6Core.so.6`void (anonymous namespace)::terminate_on_exception<QThreadPrivate::finish()::'lambda'()>(QThreadPrivate::finish()::'lambda'()&&) (.isra.0) + 105
    frame #15: 0x00007ffff76b5895 libQt6Core.so.6`set_thread_data(QThreadData*)::Cleanup::~Cleanup() + 101
    frame #16: 0x00007ffff60296c1 libc.so.6`__run_exit_handlers + 433
    frame #17: 0x00007ffff602978e libc.so.6`exit + 30
    frame #18: 0x00007ffff601024f libc.so.6`__libc_start_call_main + 127
    frame #19: 0x00007ffff601030b libc.so.6`__libc_start_main@@GLIBC_2.34 + 139
    frame #20: 0x0000555555559235 chess_studio`_start + 37
```


### Reproducible Code (if applicable)

```slint
slint code
==========
import { Button, VerticalBox, ListView, LineEdit } from "std-widgets.slint";

export component AppWindow inherits Window {
}

rust code
=========
use std::error::Error;

slint::include_modules!();

fn main() -> Result<(), Box<dyn Error>> {
    let ui = AppWindow::new()?;

    ui.run()?;

    Ok(())
}
```

### Environment Details

- Slint Version: 1.9.2
- Platform/OS: Linux Fedora 41 kde Wayland
- Programming Language: Rusr
- Backend/Renderer: Qt


### Product Impact

Not really a problem for me as it happens at the end of the application. However it could be a bigger problem.


Discussion / hints:
Happening to me as well. crashing at qt6core.so . possible stack overflow. Application is running well but the crash is concerning.
The fact that the Slint widget from a thread local data cleanup handler is most concerning.
I can reproduce the issue with Archlinux.
I believe this happens with Qt 6.8 
I See that we use `deleteLater()`. I wonder if something like this helps, if we still need the deferred deletion:

```patch
diff --git a/internal/backends/qt/lib.rs b/internal/backends/qt/lib.rs
index 38dc61fdd..e8ed44cd8 100644
--- a/internal/backends/qt/lib.rs
+++ b/internal/backends/qt/lib.rs
@@ -165,6 +165,7 @@ fn run_event_loop(&self) -> Result<(), PlatformError> {
             cpp! {unsafe [] {
                 ensure_initialized(true);
                 qApp->exec();
+                QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
             } }
             Ok(())
         }
```

Fix patch:
diff --git a/internal/backends/qt/qt_window.rs b/internal/backends/qt/qt_window.rs
index 2f5cddf2d48..143fc49fe42 100644
--- a/internal/backends/qt/qt_window.rs
+++ b/internal/backends/qt/qt_window.rs
@@ -1643,8 +1643,14 @@ cpp! {{
     {
         void operator()(QWidget *widget_ptr)
         {
-            widget_ptr->hide();
-            widget_ptr->deleteLater();
+            if (widget_ptr->parent()) {
+                // if the widget is a popup, use deleteLater (#4129)
+                widget_ptr->hide();
+                widget_ptr->deleteLater();
+            } else {
+                // Otherwise, use normal delete as it would otherwise cause crash at exit (#7570)
+                delete widget_ptr;
+            }
         }
     };
 }}


Test patch: