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/network/access/qnetworkdiskcache.cpp | |
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/network/access/qnetworkdiskcache.cpp')
-rw-r--r-- | src/network/access/qnetworkdiskcache.cpp | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/src/network/access/qnetworkdiskcache.cpp b/src/network/access/qnetworkdiskcache.cpp index 44a8298..daa2335 100644 --- a/src/network/access/qnetworkdiskcache.cpp +++ b/src/network/access/qnetworkdiskcache.cpp @@ -43,6 +43,7 @@ #include "qnetworkdiskcache.h" #include "qnetworkdiskcache_p.h" +#include "QtCore/qscopedpointer.h" #include <qfile.h> #include <qdir.h> @@ -180,8 +181,7 @@ QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData) break; } } - - QCacheItem *cacheItem = new QCacheItem; + QScopedPointer<QCacheItem> cacheItem(new QCacheItem); cacheItem->metaData = metaData; QIODevice *device = 0; @@ -190,16 +190,20 @@ QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData) device = &(cacheItem->data); } else { QString templateName = d->tmpCacheFileName(); - cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data); - if (!cacheItem->file->open()) { + QT_TRY { + cacheItem->file = new QTemporaryFile(templateName, &cacheItem->data); + } QT_CATCH(...) { + cacheItem->file = 0; + } + if (!cacheItem->file || !cacheItem->file->open()) { qWarning() << "QNetworkDiskCache::prepare() unable to open temporary file"; - delete cacheItem; + cacheItem.reset(); return 0; } cacheItem->writeHeader(cacheItem->file); device = cacheItem->file; } - d->inserting[device] = cacheItem; + d->inserting[device] = cacheItem.take(); return device; } @@ -358,31 +362,28 @@ QIODevice *QNetworkDiskCache::data(const QUrl &url) qDebug() << "QNetworkDiskCache::data()" << url; #endif Q_D(QNetworkDiskCache); - QBuffer *buffer = 0; + QScopedPointer<QBuffer> buffer; if (!url.isValid()) - return buffer; + return 0; if (d->lastItem.metaData.url() == url && d->lastItem.data.isOpen()) { - buffer = new QBuffer; + buffer.reset(new QBuffer); buffer->setData(d->lastItem.data.data()); } else { - QFile *file = new QFile(d->cacheFileName(url)); - if (!file->open(QFile::ReadOnly | QIODevice::Unbuffered)) { - delete file; + QScopedPointer<QFile> file(new QFile(d->cacheFileName(url))); + if (!file->open(QFile::ReadOnly | QIODevice::Unbuffered)) return 0; - } - if (!d->lastItem.read(file, true)) { + + if (!d->lastItem.read(file.data(), true)) { file->close(); remove(url); - delete file; return 0; } if (d->lastItem.data.isOpen()) { // compressed - buffer = new QBuffer; + buffer.reset(new QBuffer); buffer->setData(d->lastItem.data.data()); - delete file; } else { - buffer = new QBuffer; + buffer.reset(new QBuffer); // ### verify that QFile uses the fd size and not the file name qint64 size = file->size() - file->pos(); const uchar *p = 0; @@ -390,16 +391,15 @@ QIODevice *QNetworkDiskCache::data(const QUrl &url) p = file->map(file->pos(), size); #endif if (p) { - file->setParent(buffer); buffer->setData((const char *)p, size); + file.take()->setParent(buffer.data()); } else { buffer->setData(file->readAll()); - delete file; } } } buffer->open(QBuffer::ReadOnly); - return buffer; + return buffer.take(); } /*! |