diff options
author | Robert Griebl <rgriebl@trolltech.com> | 2009-06-10 11:46:23 (GMT) |
---|---|---|
committer | Robert Griebl <rgriebl@trolltech.com> | 2009-06-10 11:46:23 (GMT) |
commit | 7604f8087f88171ef933d8ae08f501467e647338 (patch) | |
tree | 51d071f462ed48d0b25884d9f62b8ba11c5dff13 /src/corelib/io | |
parent | 8c265860b41214daade7c8a28237c1e07ea71a3c (diff) | |
download | Qt-7604f8087f88171ef933d8ae08f501467e647338.zip Qt-7604f8087f88171ef933d8ae08f501467e647338.tar.gz Qt-7604f8087f88171ef933d8ae08f501467e647338.tar.bz2 |
Make Qt exception safer.
Squashed commit of the branch haralds-haralds-qt-s60-topics/topic/exceptions,
which also contains the full history.
Rev-By: Harald Fernengel
Rev-By: Ralf Engels
Diffstat (limited to 'src/corelib/io')
-rw-r--r-- | src/corelib/io/qabstractfileengine.cpp | 3 | ||||
-rw-r--r-- | src/corelib/io/qabstractfileengine.h | 4 | ||||
-rw-r--r-- | src/corelib/io/qdebug.h | 7 | ||||
-rw-r--r-- | src/corelib/io/qdir.cpp | 3 | ||||
-rw-r--r-- | src/corelib/io/qdir.h | 3 | ||||
-rw-r--r-- | src/corelib/io/qfile.cpp | 5 | ||||
-rw-r--r-- | src/corelib/io/qfileinfo.cpp | 2 | ||||
-rw-r--r-- | src/corelib/io/qfileinfo.h | 3 | ||||
-rw-r--r-- | src/corelib/io/qfilesystemwatcher_symbian.cpp | 8 | ||||
-rw-r--r-- | src/corelib/io/qfsfileengine_unix.cpp | 24 | ||||
-rw-r--r-- | src/corelib/io/qiodevice.h | 3 | ||||
-rw-r--r-- | src/corelib/io/qprocess_symbian.cpp | 1 | ||||
-rw-r--r-- | src/corelib/io/qresource.cpp | 1 | ||||
-rw-r--r-- | src/corelib/io/qresource.h | 2 | ||||
-rw-r--r-- | src/corelib/io/qsettings.cpp | 68 | ||||
-rw-r--r-- | src/corelib/io/qsettings.h | 2 | ||||
-rw-r--r-- | src/corelib/io/qsettings_p.h | 2 | ||||
-rw-r--r-- | src/corelib/io/qtextstream.cpp | 3 | ||||
-rw-r--r-- | src/corelib/io/qtextstream.h | 3 |
19 files changed, 73 insertions, 74 deletions
diff --git a/src/corelib/io/qabstractfileengine.cpp b/src/corelib/io/qabstractfileengine.cpp index bedc121..63d4944 100644 --- a/src/corelib/io/qabstractfileengine.cpp +++ b/src/corelib/io/qabstractfileengine.cpp @@ -352,8 +352,6 @@ QAbstractFileEngine::QAbstractFileEngine(QAbstractFileEnginePrivate &dd) : d_ptr */ QAbstractFileEngine::~QAbstractFileEngine() { - delete d_ptr; - d_ptr = 0; } /*! @@ -881,7 +879,6 @@ QAbstractFileEngineIterator::QAbstractFileEngineIterator(QDir::Filters filters, */ QAbstractFileEngineIterator::~QAbstractFileEngineIterator() { - delete d; } /*! diff --git a/src/corelib/io/qabstractfileengine.h b/src/corelib/io/qabstractfileengine.h index d742467..5b75343 100644 --- a/src/corelib/io/qabstractfileengine.h +++ b/src/corelib/io/qabstractfileengine.h @@ -193,7 +193,7 @@ protected: QAbstractFileEngine(); QAbstractFileEngine(QAbstractFileEnginePrivate &); - QAbstractFileEnginePrivate *d_ptr; + QScopedPointer<QAbstractFileEnginePrivate> d_ptr; private: Q_DECLARE_PRIVATE(QAbstractFileEngine) Q_DISABLE_COPY(QAbstractFileEngine) @@ -237,7 +237,7 @@ private: friend class QDirIterator; friend class QDirIteratorPrivate; void setPath(const QString &path); - QAbstractFileEngineIteratorPrivate *d; + QScopedPointer<QAbstractFileEngineIteratorPrivate> d; }; QT_END_NAMESPACE diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 8334146..60dc688 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -79,8 +79,11 @@ public: inline QDebug &operator=(const QDebug &other); inline ~QDebug() { if (!--stream->ref) { - if(stream->message_output) - qt_message_output(stream->type, stream->buffer.toLocal8Bit().data()); + if(stream->message_output) { + QT_TRY { + qt_message_output(stream->type, stream->buffer.toLocal8Bit().data()); + } QT_CATCH(std::bad_alloc) { /* We're out of memory - give up. */ } + } delete stream; } } diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 901641c..e1cc7ac 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -83,6 +83,7 @@ class QDirPrivate QDir *q_ptr; Q_DECLARE_PUBLIC(QDir) + friend class QScopedPointer<QDirPrivate>; protected: QDirPrivate(QDir*, const QDir *copy=0); ~QDirPrivate(); @@ -573,8 +574,6 @@ QDir::QDir(const QDir &dir) : d_ptr(new QDirPrivate(this, &dir)) QDir::~QDir() { - delete d_ptr; - d_ptr = 0; } /*! diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h index c4f6b1a..ac80094 100644 --- a/src/corelib/io/qdir.h +++ b/src/corelib/io/qdir.h @@ -45,6 +45,7 @@ #include <QtCore/qstring.h> #include <QtCore/qfileinfo.h> #include <QtCore/qstringlist.h> +#include <QtCore/qscopedpointer.h> QT_BEGIN_HEADER @@ -57,7 +58,7 @@ class QDirPrivate; class Q_CORE_EXPORT QDir { protected: - QDirPrivate *d_ptr; + QScopedPointer<QDirPrivate> d_ptr; private: Q_DECLARE_PRIVATE(QDir) public: diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index 551485d..1e3d5a3 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -109,6 +109,7 @@ QFilePrivate::openExternalFile(int flags, int fd) return false; #else delete fileEngine; + fileEngine = 0; QFSFileEngine *fe = new QFSFileEngine; fe->setFileName(fileName); fileEngine = fe; @@ -125,6 +126,7 @@ QFilePrivate::openExternalFile(int flags, FILE *fh) return false; #else delete fileEngine; + fileEngine = 0; QFSFileEngine *fe = new QFSFileEngine; fe->setFileName(fileName); fileEngine = fe; @@ -410,9 +412,6 @@ QFile::QFile(QFilePrivate &dd, QObject *parent) QFile::~QFile() { close(); -#ifdef QT_NO_QOBJECT - delete d_ptr; -#endif } /*! diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index 96e0f82..b36fb69 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -439,8 +439,6 @@ QFileInfo::QFileInfo(const QFileInfo &fileinfo) : d_ptr(new QFileInfoPrivate(&fi QFileInfo::~QFileInfo() { - delete d_ptr; - d_ptr = 0; } /*! diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 97997a3..6c01f63 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -44,6 +44,7 @@ #include <QtCore/qfile.h> #include <QtCore/qlist.h> +#include <QtCore/qscopedpointer.h> QT_BEGIN_HEADER @@ -165,7 +166,7 @@ public: #endif protected: - QFileInfoPrivate *d_ptr; + QScopedPointer<QFileInfoPrivate> d_ptr; private: Q_DECLARE_PRIVATE(QFileInfo) }; diff --git a/src/corelib/io/qfilesystemwatcher_symbian.cpp b/src/corelib/io/qfilesystemwatcher_symbian.cpp index b1a6188..aeb19db 100644 --- a/src/corelib/io/qfilesystemwatcher_symbian.cpp +++ b/src/corelib/io/qfilesystemwatcher_symbian.cpp @@ -80,7 +80,7 @@ void CNotifyChangeEvent::RunL() if (iStatus.Int() == KErrNone){ fsSession.NotifyChange(ENotifyAll, iStatus, watchedPath); SetActive(); - engine->emitPathChanged(this); + QT_TRANSLATE_EXCEPTION_TO_SYMBIAN_LEAVE(engine->emitPathChanged(this)); } else { qWarning("CNotifyChangeEvent::RunL() - Failed to order change notifications: %d", iStatus.Int()); } @@ -262,9 +262,9 @@ void QSymbianFileSystemWatcherEngine::run() void QSymbianFileSystemWatcherEngine::addNativeListener(const QString &directoryPath) { QMutexLocker locker(&mutex); - HBufC* buffer = qt_QString2HBufCNewL(QDir::toNativeSeparators(directoryPath)); - currentEvent = CNotifyChangeEvent::New(fsSession, *buffer, this); - delete buffer; + QString nativeDir(QDir::toNativeSeparators(directoryPath)); + TPtrC ptr(qt_QString2TPtrC(nativeDir)); + currentEvent = CNotifyChangeEvent::New(fsSession, ptr, this); syncCondition.wakeOne(); } diff --git a/src/corelib/io/qfsfileengine_unix.cpp b/src/corelib/io/qfsfileengine_unix.cpp index e595f15..45e547f 100644 --- a/src/corelib/io/qfsfileengine_unix.cpp +++ b/src/corelib/io/qfsfileengine_unix.cpp @@ -404,23 +404,21 @@ bool QFSFileEngine::copy(const QString &newName) TInt err = rfs.Connect(); if (err == KErrNone) { CFileMan* fm = NULL; - HBufC* oldBuf = NULL; - HBufC* newBuf = NULL; + QString oldNative(QDir::toNativeSeparators(d->filePath)); + TPtrC oldPtr(qt_QString2TPtrC(oldNative)); + QFileInfo fi(newName); + QString absoluteNewName = fi.absolutePath() + QDir::separator() + fi.fileName(); + QString newNative(QDir::toNativeSeparators(absoluteNewName)); + TPtrC newPtr(qt_QString2TPtrC(newNative)); TRAP (err, fm = CFileMan::NewL(rfs); - oldBuf = qt_QString2HBufCNewL(QDir::toNativeSeparators(d->filePath)); RFile rfile; - err = rfile.Open(rfs, *oldBuf, EFileShareReadersOrWriters); + err = rfile.Open(rfs, oldPtr, EFileShareReadersOrWriters); if (err == KErrNone) { - QFileInfo fi(newName); - QString absoluteNewName = fi.absolutePath() + QDir::separator() + fi.fileName(); - newBuf = qt_QString2HBufCNewL(QDir::toNativeSeparators(absoluteNewName)); - err = fm->Copy(rfile, *newBuf); + err = fm->Copy(rfile, newPtr); rfile.Close(); } ) // End TRAP - delete oldBuf; - delete newBuf; delete fm; rfs.Close(); } @@ -675,10 +673,10 @@ static bool _q_isSymbianHidden(const QString &path, bool isDir) if (isDir && absPath.at(absPath.size()-1) != QChar('/')) { absPath += QChar('/'); } - HBufC* buffer = qt_QString2HBufCNewL(QDir::toNativeSeparators(absPath)); + QString native(QDir::toNativeSeparators(absPath)); + TPtrC ptr(qt_QString2TPtrC(native)); TUint attributes; - err = rfs.Att(*buffer, attributes); - delete buffer; + err = rfs.Att(ptr, attributes); rfs.Close(); if (err == KErrNone && (attributes & KEntryAttHidden)) { retval = true; diff --git a/src/corelib/io/qiodevice.h b/src/corelib/io/qiodevice.h index 76d47ca..ad87125 100644 --- a/src/corelib/io/qiodevice.h +++ b/src/corelib/io/qiodevice.h @@ -46,6 +46,7 @@ #include <QtCore/qobject.h> #else #include <QtCore/qobjectdefs.h> +#include <QtCore/qscopedpointer.h> #endif #include <QtCore/qstring.h> @@ -160,7 +161,7 @@ protected: void setErrorString(const QString &errorString); #ifdef QT_NO_QOBJECT - QIODevicePrivate *d_ptr; + QScopedPointer<QIODevicePrivate> d_ptr; #endif private: diff --git a/src/corelib/io/qprocess_symbian.cpp b/src/corelib/io/qprocess_symbian.cpp index a59976c..26e7cdc 100644 --- a/src/corelib/io/qprocess_symbian.cpp +++ b/src/corelib/io/qprocess_symbian.cpp @@ -56,6 +56,7 @@ User::Panic(KQProcessPanic, panicReason); \ } +#include <exception> #include <e32base.h> #include <e32std.h> #include <stdio.h> diff --git a/src/corelib/io/qresource.cpp b/src/corelib/io/qresource.cpp index 1f77caa..0cbcc41 100644 --- a/src/corelib/io/qresource.cpp +++ b/src/corelib/io/qresource.cpp @@ -334,7 +334,6 @@ QResource::QResource(const QString &file, const QLocale &locale) : d_ptr(new QRe */ QResource::~QResource() { - delete d_ptr; } /*! diff --git a/src/corelib/io/qresource.h b/src/corelib/io/qresource.h index 1e9b2d5..f260b68 100644 --- a/src/corelib/io/qresource.h +++ b/src/corelib/io/qresource.h @@ -91,7 +91,7 @@ protected: QStringList children() const; protected: - QResourcePrivate *d_ptr; + QScopedPointer<QResourcePrivate> d_ptr; private: Q_DECLARE_PRIVATE(QResource) diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 6152518..afaaf28 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -1106,10 +1106,10 @@ static QString getPath(QSettings::Format format, QSettings::Scope scope) QString homePath = QDir::homePath(); QString systemPath; - globalMutex()->lock(); + QMutexLocker locker(globalMutex()); PathHash *pathHash = pathHashFunc(); bool loadSystemPath = pathHash->isEmpty(); - globalMutex()->unlock(); + locker.unlock(); if (loadSystemPath) { /* @@ -1121,7 +1121,7 @@ static QString getPath(QSettings::Format format, QSettings::Scope scope) systemPath += QLatin1Char('/'); } - QMutexLocker locker(globalMutex()); + locker.relock(); if (pathHash->isEmpty()) { /* Lazy initialization of pathHash. We initialize the @@ -1181,9 +1181,6 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format, int i; initFormat(); - for (i = 0; i < NumConfFiles; ++i) - confFiles[i] = 0; - QString org = organization; if (org.isEmpty()) { setStatus(QSettings::AccessError); @@ -1196,14 +1193,14 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(QSettings::Format format, if (scope == QSettings::UserScope) { QString userPath = getPath(format, QSettings::UserScope); if (!application.isEmpty()) - confFiles[F_User | F_Application] = QConfFile::fromName(userPath + appFile, true); - confFiles[F_User | F_Organization] = QConfFile::fromName(userPath + orgFile, true); + confFiles[F_User | F_Application].reset(QConfFile::fromName(userPath + appFile, true)); + confFiles[F_User | F_Organization].reset(QConfFile::fromName(userPath + orgFile, true)); } QString systemPath = getPath(format, QSettings::SystemScope); if (!application.isEmpty()) - confFiles[F_System | F_Application] = QConfFile::fromName(systemPath + appFile, false); - confFiles[F_System | F_Organization] = QConfFile::fromName(systemPath + orgFile, false); + confFiles[F_System | F_Application].reset(QConfFile::fromName(systemPath + appFile, false)); + confFiles[F_System | F_Organization].reset(QConfFile::fromName(systemPath + orgFile, false)); for (i = 0; i < NumConfFiles; ++i) { if (confFiles[i]) { @@ -1222,9 +1219,7 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName, { initFormat(); - confFiles[0] = QConfFile::fromName(fileName, true); - for (int i = 1; i < NumConfFiles; ++i) - confFiles[i] = 0; + confFiles[0].reset(QConfFile::fromName(fileName, true)); initAccess(); } @@ -1241,19 +1236,27 @@ QConfFileSettingsPrivate::~QConfFileSettingsPrivate() usedHash->remove(confFiles[i]->name); if (confFiles[i]->size == 0) { - delete confFiles[i]; + delete confFiles[i].take(); } else if (unusedCache) { - // compute a better size? - unusedCache->insert(confFiles[i]->name, confFiles[i], + QT_TRY { + // compute a better size? + unusedCache->insert(confFiles[i]->name, confFiles[i].data(), 10 + (confFiles[i]->originalKeys.size() / 4)); + confFiles[i].take(); + } QT_CATCH(...) { + // out of memory. Do not cache the file. + delete confFiles[i].take(); + } } } + // prevent the ScopedPointer to deref it again. + confFiles[i].take(); } } void QConfFileSettingsPrivate::remove(const QString &key) { - QConfFile *confFile = confFiles[spec]; + QConfFile *confFile = confFiles[spec].data(); if (!confFile) return; @@ -1280,7 +1283,7 @@ void QConfFileSettingsPrivate::remove(const QString &key) void QConfFileSettingsPrivate::set(const QString &key, const QVariant &value) { - QConfFile *confFile = confFiles[spec]; + QConfFile *confFile = confFiles[spec].data(); if (!confFile) return; @@ -1297,7 +1300,7 @@ bool QConfFileSettingsPrivate::get(const QString &key, QVariant *value) const bool found = false; for (int i = 0; i < NumConfFiles; ++i) { - if (QConfFile *confFile = confFiles[i]) { + if (QConfFile *confFile = confFiles[i].data()) { QMutexLocker locker(&confFile->mutex); if (!confFile->addedKeys.isEmpty()) { @@ -1332,7 +1335,7 @@ QStringList QConfFileSettingsPrivate::children(const QString &prefix, ChildSpec int startPos = prefix.size(); for (int i = 0; i < NumConfFiles; ++i) { - if (QConfFile *confFile = confFiles[i]) { + if (QConfFile *confFile = confFiles[i].data()) { QMutexLocker locker(&confFile->mutex); if (thePrefix.isEmpty()) { @@ -1365,7 +1368,7 @@ QStringList QConfFileSettingsPrivate::children(const QString &prefix, ChildSpec void QConfFileSettingsPrivate::clear() { - QConfFile *confFile = confFiles[spec]; + QConfFile *confFile = confFiles[spec].data(); if (!confFile) return; @@ -1381,7 +1384,7 @@ void QConfFileSettingsPrivate::sync() // error we just try to go on and make the best of it for (int i = 0; i < NumConfFiles; ++i) { - QConfFile *confFile = confFiles[i]; + QConfFile *confFile = confFiles[i].data(); if (confFile) { QMutexLocker locker(&confFile->mutex); syncConfFile(i); @@ -1396,7 +1399,7 @@ void QConfFileSettingsPrivate::flush() QString QConfFileSettingsPrivate::fileName() const { - QConfFile *confFile = confFiles[spec]; + QConfFile *confFile = confFiles[spec].data(); if (!confFile) return QString(); return confFile->name; @@ -1407,7 +1410,7 @@ bool QConfFileSettingsPrivate::isWritable() const if (format > QSettings::IniFormat && !writeFunc) return false; - QConfFile *confFile = confFiles[spec]; + QConfFile *confFile = confFiles[spec].data(); if (!confFile) return false; @@ -1416,7 +1419,7 @@ bool QConfFileSettingsPrivate::isWritable() const void QConfFileSettingsPrivate::syncConfFile(int confFileNo) { - QConfFile *confFile = confFiles[confFileNo]; + QConfFile *confFile = confFiles[confFileNo].data(); bool readOnly = confFile->addedKeys.isEmpty() && confFile->removedKeys.isEmpty(); bool ok; @@ -2759,11 +2762,13 @@ QSettings::QSettings(const QString &fileName, Format format) QSettings::~QSettings() { Q_D(QSettings); - if (d->pendingChanges) - d->flush(); -#ifdef QT_NO_QOBJECT - delete d; -#endif + if (d->pendingChanges) { + QT_TRY { + d->flush(); + } QT_CATCH(...) { + ; // ok. then don't flush but at least don't throw in the destructor + } + } } /*! @@ -3566,8 +3571,7 @@ void QSettings::setPath_helper(Scope scope, const QString &organization, const Q QSettingsPrivate *oldPriv = d; QSettingsPrivate *newPriv = QSettingsPrivate::create(oldPriv->format, scope, organization, application); static_cast<QObjectPrivate &>(*newPriv) = static_cast<QObjectPrivate &>(*oldPriv); // copy the QObject stuff over (hack) - delete oldPriv; - d_ptr = newPriv; + d_ptr.reset(newPriv); } /*! \fn bool QSettings::writeEntry(const QString &key, bool value) diff --git a/src/corelib/io/qsettings.h b/src/corelib/io/qsettings.h index cbb24ba..41bce89 100644 --- a/src/corelib/io/qsettings.h +++ b/src/corelib/io/qsettings.h @@ -78,7 +78,7 @@ class Q_CORE_EXPORT QSettings #ifndef QT_NO_QOBJECT Q_OBJECT #else - QSettingsPrivate *d_ptr; + QScopedPointer<QSettingsPrivate> d_ptr; #endif Q_DECLARE_PRIVATE(QSettings) diff --git a/src/corelib/io/qsettings_p.h b/src/corelib/io/qsettings_p.h index dd72fd9..1bdbf2c 100644 --- a/src/corelib/io/qsettings_p.h +++ b/src/corelib/io/qsettings_p.h @@ -300,7 +300,7 @@ private: void ensureAllSectionsParsed(QConfFile *confFile) const; void ensureSectionParsed(QConfFile *confFile, const QSettingsKey &key) const; - QConfFile *confFiles[NumConfFiles]; + QScopedSharedPointer<QConfFile> confFiles[NumConfFiles]; QSettings::ReadFunc readFunc; QSettings::WriteFunc writeFunc; QString extension; diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index 1167671..9382fae 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -1112,9 +1112,6 @@ QTextStream::~QTextStream() #endif if (!d->writeBuffer.isEmpty()) d->flushWriteBuffer(); - - delete d; - d_ptr = 0; } /*! diff --git a/src/corelib/io/qtextstream.h b/src/corelib/io/qtextstream.h index 6302d34..cfacec6 100644 --- a/src/corelib/io/qtextstream.h +++ b/src/corelib/io/qtextstream.h @@ -46,6 +46,7 @@ #include <QtCore/qstring.h> #include <QtCore/qchar.h> #include <QtCore/qlocale.h> +#include <QtCore/qscopedpointer.h> #ifndef QT_NO_TEXTCODEC # ifdef QT3_SUPPORT @@ -256,7 +257,7 @@ private: Q_DISABLE_COPY(QTextStream) - QTextStreamPrivate *d_ptr; + QScopedPointer<QTextStreamPrivate> d_ptr; }; Q_DECLARE_OPERATORS_FOR_FLAGS(QTextStream::NumberFlags) |