From cf48eded4dc0e6c860b48a1d756b5920ffc6a724 Mon Sep 17 00:00:00 2001 From: "Bradley T. Hughes" Date: Tue, 8 May 2012 10:29:45 +0200 Subject: [PATCH] Don't leak native dialog resources Have QDialog::~QDialog() call deleteNativeDialog_sys() on the helpers, so that we don't risk leaking any resources allocated in the helper. QFileDialog does this now, but not QColorDialog or QFontDialog. The Cocoa plugin worked around this problem by calling deleteNativeDialog_sys() itself, but the Windows plugin does not do this, resulting in leaks. Change-Id: I380d87c95686c8f3cb260f9242299be27329280d Reviewed-by: Friedemann Kleint --- src/plugins/platforms/cocoa/qcocoacolordialoghelper.mm | 4 +--- src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm | 4 +--- src/widgets/dialogs/qdialog.cpp | 10 ++++++++++ src/widgets/dialogs/qdialog_p.h | 1 + src/widgets/dialogs/qfiledialog.cpp | 8 +++----- src/widgets/dialogs/qfiledialog_p.h | 10 ---------- 6 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoacolordialoghelper.mm b/src/plugins/platforms/cocoa/qcocoacolordialoghelper.mm index 9f4446b32e..45c36b4e1e 100644 --- a/src/plugins/platforms/cocoa/qcocoacolordialoghelper.mm +++ b/src/plugins/platforms/cocoa/qcocoacolordialoghelper.mm @@ -343,9 +343,7 @@ QCocoaColorDialogHelper::QCocoaColorDialogHelper() : } QCocoaColorDialogHelper::~QCocoaColorDialogHelper() -{ - deleteNativeDialog_sys(); -} +{ } void QCocoaColorDialogHelper::platformNativeDialogModalHelp() { diff --git a/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm b/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm index 515bc2a6ee..5f39531503 100644 --- a/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm +++ b/src/plugins/platforms/cocoa/qcocoafontdialoghelper.mm @@ -362,9 +362,7 @@ QCocoaFontDialogHelper::QCocoaFontDialogHelper() : } QCocoaFontDialogHelper::~QCocoaFontDialogHelper() -{ - deleteNativeDialog_sys(); -} +{ } void QCocoaFontDialogHelper::platformNativeDialogModalHelp() { diff --git a/src/widgets/dialogs/qdialog.cpp b/src/widgets/dialogs/qdialog.cpp index 82a7b1a30d..44463b8d32 100644 --- a/src/widgets/dialogs/qdialog.cpp +++ b/src/widgets/dialogs/qdialog.cpp @@ -132,6 +132,14 @@ QVariant QDialogPrivate::styleHint(QPlatformDialogHelper::StyleHint hint) const return QPlatformDialogHelper::defaultStyleHint(hint); } +void QDialogPrivate::deleteNativeDialog() +{ + if (QPlatformDialogHelper *helper = platformHelper()) { + helper->deleteNativeDialog_sys(); + nativeDialogInUse = false; + } +} + /*! \class QDialog \brief The QDialog class is the base class of dialog windows. @@ -328,6 +336,7 @@ QDialog::QDialog(QDialogPrivate &dd, QWidget *parent, Qt::WindowFlags f) QDialog::~QDialog() { + Q_D(QDialog); QT_TRY { // Need to hide() here, as our (to-be) overridden hide() // will not be called in ~QWidget. @@ -335,6 +344,7 @@ QDialog::~QDialog() } QT_CATCH(...) { // we're in the destructor - just swallow the exception } + d->deleteNativeDialog(); } /*! diff --git a/src/widgets/dialogs/qdialog_p.h b/src/widgets/dialogs/qdialog_p.h index 8c53d6a404..7b02f359dd 100644 --- a/src/widgets/dialogs/qdialog_p.h +++ b/src/widgets/dialogs/qdialog_p.h @@ -84,6 +84,7 @@ public: QWindow *parentWindow() const; bool setNativeDialogVisible(bool visible); QVariant styleHint(QPlatformDialogHelper::StyleHint hint) const; + void deleteNativeDialog(); QPointer mainDef; Qt::Orientation orientation; diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 3908daec9c..15a85c98d9 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -362,13 +362,11 @@ QFileDialog::QFileDialog(const QFileDialogArgs &args) */ QFileDialog::~QFileDialog() { - Q_D(QFileDialog); #ifndef QT_NO_SETTINGS QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("Qt")); settings.setValue(QLatin1String("filedialog"), saveState()); #endif - d->deleteNativeDialog_sys(); } /*! @@ -1372,10 +1370,10 @@ void QFileDialog::setAcceptMode(QFileDialog::AcceptMode mode) d->qFileDialogUi->lookInCombo->setEditable(false); } d->retranslateWindowTitle(); -#if defined(Q_OS_MAC) - d->deleteNativeDialog_sys(); + // we need to recreate the native dialog when changing the AcceptMode + d->deleteNativeDialog(); + // clear WA_DontShowOnScreen so that d->canBeNativeDialog() doesn't return false incorrectly setAttribute(Qt::WA_DontShowOnScreen, false); -#endif } /* diff --git a/src/widgets/dialogs/qfiledialog_p.h b/src/widgets/dialogs/qfiledialog_p.h index a66ee757f6..4283842b1d 100644 --- a/src/widgets/dialogs/qfiledialog_p.h +++ b/src/widgets/dialogs/qfiledialog_p.h @@ -244,7 +244,6 @@ public: // dialog. Returning false means that a non-native dialog must be // used instead. bool canBeNativeDialog(); - void deleteNativeDialog_sys(); QDialog::DialogCode dialogResultCode_sys(); void setDirectory_sys(const QString &directory); @@ -347,15 +346,6 @@ inline QString QFileDialogPrivate::rootPath() const { return model->rootPath(); } -// Dummies for platforms that don't use native dialogs: -inline void QFileDialogPrivate::deleteNativeDialog_sys() -{ - if (QPlatformFileDialogHelper *helper = platformFileDialogHelper()) { - helper->deleteNativeDialog_sys(); - nativeDialogInUse = false; - } -} - inline QDialog::DialogCode QFileDialogPrivate::dialogResultCode_sys() { QDialog::DialogCode result = QDialog::Rejected;