Details
-
Bug
-
Status: Closed
-
P1: Critical
-
Resolution: Done
-
5.12.5
-
None
-
xcode 11.1, macOS Mojave
Description
If you reuse a widget that's closed (a QDialog), and you then access its winId, it will likely crash
#include <QtWidgets> #import <AppKit/AppKit.h> class MyWidget : public QDialog { bool event(QEvent *event) override { if (event->type() == QEvent::Show) { qDebug() << "fixTitleBar called: WinID" << winId(); NSView* mainView = reinterpret_cast<NSView *>(winId()); NSLog(@"mainView %@", mainView); //accessing the bad reference } return QDialog::event(event); } }; int main(int argc, char **argv) { QApplication app(argc, argv); MyWidget w; qDebug() << 1 << w.exec(); qDebug() << 2 << w.exec(); return 0; }
On the second call, the winId is still the same but it crashes as soon as you use winId as an NSView (dangling pointer?).
A workaround is to define the close event as follows
void closeEvent(QCloseEvent *e) override { e->ignore(); hide(); }
But I'm not willing to do that since the implications are probably dear.
NOTE: With Qt 5.9 this code is working fine and the reference is the also the same on the 2 calls.